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

From perlre:

+ Match 1 or more times {n}+ Match exactly n times and give nothing back (redundant)

I take the "redundant" annotation to mean that it is the same as:

+? Match 1 or more times, not greedily

Though "not greedily" seems to conflict with "or more"?


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!

Replies are listed 'Best First'.
Re^3: about style: use possessive or atomic?
by rsFalse (Chaplain) on Aug 16, 2015 at 11:38 UTC
    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?

      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!
      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).
Re^3: about style: use possessive or atomic?
by LanX (Saint) on Aug 16, 2015 at 11:33 UTC
    I just ignored that the + is a possessive quantifier in this combination.

    But its still redundant like the docs you cited say, backtracking wouldn't change without the plus. 1

    (as far as I understand the docs with my pre coffee brain and without possibility to test on my mobile)

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

    1) no that's not true, see discussion further down!