Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Perl Complains of Nested Quantifiers

by Kenosis (Priest)
on May 22, 2012 at 20:01 UTC ( #971864=note: print w/ replies, xml ) Need Help??


in reply to Re: Perl Complains of Nested Quantifiers
in thread Perl Complains of Nested Quantifiers

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.


Comment on Re^2: Perl Complains of Nested Quantifiers
Download Code
Re^3: Perl Complains of Nested Quantifiers
by demerphq (Chancellor) on May 22, 2012 at 21:38 UTC

    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

Re^3: Perl Complains of Nested Quantifiers
by AnomalousMonk (Abbot) on May 22, 2012 at 23:14 UTC
    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://971864]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-09-23 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (239 votes), past polls