in reply to Re^2: about style: use possessive or atomic?
in thread about style: use possessive or atomic?

chomp( $match = <DATA> ); chomp( $_ = <DATA> ); for my $regex ( "($match){1}+", "($match)+", "($match)+?", ){ print qw(FAIL SUCCESS)[ !! m/\A $regex \z/x ], "\n" } __DATA__ (a|b){2} bbab __OUTPUT__ FAIL SUCCESS SUCCESS
Is it true that ($match){1}+ equals to simply $match?

Replies are listed 'Best First'.
Re^4: about style: use possessive or atomic?
by Athanasius (Archbishop) on Aug 16, 2015 at 12:23 UTC

    Hello rsFalse,

    Is it true that ($match){1}+ equals to simply $match?

    Yes, and the + does nothing here, as you can see with use warnings:

    Output:

    22:21 >perl 1345_SoPW.pl Useless use of greediness modifier '+' in regex; marked by <-- HERE in + m/\A ((a|b){2}){1}+ <-- HERE \z/ at 1345_SoPW.pl line 27, <DATA> li +ne 2. FAIL FAIL FAIL SUCCESS SUCCESS 22:21 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Is it true that ($match){1}+ equals to simply $match? Yes, and the + does nothing here, as you can see with use warnings:

      That's only true because you the OP included a quantifier in the embedded regex; and you get 3 fails because your anchors guarantee the regex used couldn't succeed (which doesn't seem like a useful test?)

      This shows that {n}+ is a valid quantifier:

      use strict; use warnings; chomp(my $match = <DATA>); chomp( $_ = <DATA>); for my $regex ( "($match){1}+", "($match){1}", "($match)", "($match)+", "($match)+?", ) { print qw(FAIL SUCCESS)[ !! m/$regex/ ], "\n" } __DATA__ (?:a|b) bbab
      C:\test>junk37 SUCCESS SUCCESS SUCCESS SUCCESS SUCCESS

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
      I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!

        Hello BrowserUk,

        I still get the warning when the regex isn’t embedded:

        23:15 >perl -wE "my $r = qr{(?:a|b)}; say $r;" (?^u:(?:a|b)) 23:17 >perl -wE "my $r = qr{(?:a|b){1}}; say $r;" (?^u:(?:a|b){1}) 23:17 >perl -wE "my $r = qr{(?:a|b){1}+}; say $r;" Useless use of greediness modifier '+' in regex; marked by <-- HERE in + m/(?:a|b){1}+ <-- HERE / at -e line 1. (?^u:(?:a|b){1}+) 23:17 >perl -v This is perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x +64-multi-thread ...

        I get the same warning with Strawberry Perl v5.20.2, but not with v5.18.2. Is the warning erroneous?

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^4: about style: use possessive or atomic?
by LanX (Saint) on Aug 16, 2015 at 12:42 UTC
    The whole discussion is quite confusing because greediness and atomic grouping are messed up.

    Could you please show a use case where you want to avoid backtracking into a once matched group (thats what atomic/possessive is about)

    answering your question

    > Is it true that ($match){1}+ equals to simply $match?

    No!

    DB<125> $_ = "a"x5 => "aaaaa" DB<126> $match = "(a+)" => "(a+)" DB<128> $_ =~ /${match}a/ => "aaaa" DB<129> $_ =~ /${match}{1}a/ => "aaaa" DB<130> $_ =~ /${match}{1}+a/ DB<131> $_ =~ /(?>${match})a/ DB<132>

    It seems to show that perlre is wrong with the redundant anotation.¹

    edit

    Answering your root question, IMHO using + as a second quantifier like in a++ only makes sense if you already need to use a first quantifier in the first level of interpolation, like in

    ($pat1)++($pat2)*+

    i.e. + and * are not part of a sub-pattern $patN.

    Otherwise I'd always prefer

    (?>$pat1)(?>$pat2)

    using something like ($pat){1}+ seems quite confusing for me.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    ¹) if "redundant" is supposed to mean line 131 and 130 are equivalent, then redundant is a misleading wording

      I used a bigger regex, which contained some such groups. And for avoiding unexpected results, I wanted to use atomics/possessives. And I wanted to capture these groups. So instead of writing /(?> ($pat1) ) (?> ($pat2) )/x I decided to use possessive quantifiers to increase readability :) (my eyes dislike too many parentheses)
      I don't have access to code, where I used it, but friend wrote to me, that with perl 5.20 he gained something wrong (warning/error).
        > but friend wrote to me, that with perl 5.20 he gained something wrong (warning/error).

        Couldn't you make this clear from the beginning?

        > don't have access to code,

        Then please reproduce it next time.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!