develooper Front page | perl.perl5.porters | Postings from November 2003

Re: [perl #24440] 'nested regex without backtracking' bug: here is minimal working example

Thread Previous
From:
Rick Delaney
Date:
November 8, 2003 06:47
Subject:
Re: [perl #24440] 'nested regex without backtracking' bug: here is minimal working example
Message ID:
20031108094718.K13585@biff.bort.ca
On Fri, Nov 07, 2003 at 03:51:02PM -0000, Mihail Egorov wrote:
> 
> #!/usr/bin/perl -w
> 
> print 1 if "ApqZ" =~ /A (   (?> [^p]* ) | (?> p[^pZ]* )  ) Z/x;
> print 2 if "ApqZ" =~ /A (?> (?> [^p]* ) | (?> p[^pZ]* )  ) Z/x;
> print 3 if "ApqZ" =~ /A (?>               (?> p[^pZ]* )  ) Z/x;
> 
> print "\n   This program has to print '123', and really prints only '13'." ;
> print "\n   This is considered to be a 'nested regex without backtracking' bug." ;
> print "\n";

This behaviour is correct.  After the "A", the regexp engine
tries to match as many non-p's as possible and succeeds, matching zero
of them.  The surrounding (?> ) means that the first thing matched is
the only thing that can be matched at that position.  Since the "Z"
doesn't follow the "A" the whole pattern fails.

Once a pattern in (?>pattern) is successful there are no second chances.
So if (?>A|B) can match A then it will never try to match B no matter
what the rest of the pattern is.

-- 
Rick Delaney
rick@bort.ca

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About