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

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:

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/\A $regex \z/x ], "\n" } __DATA__ (a|b){2} bbab

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,

Replies are listed 'Best First'.
Re^5: about style: use possessive or atomic?
by BrowserUk (Patriarch) on Aug 16, 2015 at 13:00 UTC
    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,

        I get the same warning with Strawberry Perl v5.20.2, but not on v5.18.2.

        You're right! My default perl is currently 5.18:

        C:\test>\perl5.18\perl\bin\perl.exe junk37.pl SUCCESS SUCCESS SUCCESS SUCCESS SUCCESS C:\test>\Perl5.20\bin\perl.exe junk37.pl Useless use of greediness modifier '+' in regex; marked by <-- HERE in + m/((?:a|b)){1}+ <-- HERE / at junk37.pl line 15, <DATA> line 2. SUCCESS SUCCESS SUCCESS SUCCESS SUCCESS C:\test>\Perl22\bin\perl.exe junk37.pl Useless use of greediness modifier '+' in regex; marked by <-- HERE in + m/((?:a|b)){1}+ <-- HERE / at junk37.pl line 15, <DATA> line 2. SUCCESS SUCCESS SUCCESS SUCCESS SUCCESS
        Is the warning erroneous?

        Erm. I was relying on Perl to tell me :)

        The fact that the warning has been recently added suggests it is probably correct.

        That said, the construct is still listed in the 5.22 docs:  {n}+   Match exactly n times and give nothing back (redundant); which basically means your guess is as good as anybody's at this point?


        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!
        > Is the warning erroneous?

        if the meaning of {n}+ hasn't be deprecated or changed its a totally wrong warning.

        Please run my code with 5.20, with same results we should file a bug report.

        edit

        As you can see there its not redundant.

        The plus is neither a greedyness operator here, bc its in second place, its there to stop backtracking.

        update
        Apparently adding the second + turns the whole quantifier {n}+ into a possessive quantifier.

        see here

        Like a greedy quantifier, a possessive quantifier repeats the token as many times as possible. Unlike a greedy quantifier, it does not give up matches as the engine backtracks. With a possessive quantifier, the deal is all or nothing. You can make a quantifier possessive by placing an extra + after it. * is greedy, *? is lazy, and *+ is possessive. ++, ?+ and {n,m}+ are all possessive as well.

        so + is "greedy" and ++ "possessive", {n}+ is analogous to ++ .

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