Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Perl Complains of Nested Quantifiers

by AnomalousMonk (Abbot)
on May 22, 2012 at 19:25 UTC ( #971857=note: print w/ replies, xml ) Need Help??


in reply to Perl Complains of Nested Quantifiers

The 'atomic' extended pattern  "(?>pattern)" (available in 5.8) will give you possessiveness around the entire pattern or any sub-pattern of your choosing. See Extended Patterns in perlre. (The following example doesn't really illustrate atomic/possessive matching; for a good discussion and pertinent examples, see the docs.)

>perl -wMstrict -le "print qq{perl ver. $]}; my $rx = qr{ ((?> \d+)) }xms; 'abc1234def' =~ $rx; print qq{'$1'}; " perl ver. 5.008009 '1234'


Comment on Re: Perl Complains of Nested Quantifiers
Select or Download Code
Re^2: Perl Complains of Nested Quantifiers
by Kenosis (Priest) on May 22, 2012 at 20:01 UTC

    The Extended Patterns documentation mentions that "(?>pattern) does not disable backtracking altogether once it has matched." A concern was expressed about "catastrophic backtracking." I'm curious about the nature of the 'catastrophe,' and whether your solution would satisfactorily avert it.

      Well, what this means is that a pattern like /foo(?:baz)++baz/ will never match. If you had a string like "foobazbazbaz" it would fail because (?:baz)++ will "gobble up" all of the "baz" in the string, and then refuse to give anything back. What it means by "does not disable backtracking" is that "foobazbazbaz foobazbazbaz" will *attempt* the /(?:baz)++/ twice, once after each "foo". Neither will match of course, and if the RE was _really_ smart it would know it could never match and wouldn't try at all, but it isn't. :-)

      ---
      $world=~s/war/peace/g

      A concern was expressed about "catastrophic backtracking." I'm curious about ... whether your solution would satisfactorily avert it.

      It would not. AFAIU, possessive quantifiers like  ++ *+ ?+ {n,m}+ are just special, limited cases of the general  (?>...) atomic grouping. See example below.

      >perl -wMstrict -le "my $s = 'foobazbazbaz foobazbazbaz'; print 'match 1' if $s =~ m{ foo (?> (?:baz)+) baz }xms; print 'match 2' if $s =~ m{ foo (?> .* baz) baz }xms; "
      (no output)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://971857]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-11-22 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (118 votes), past polls