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

Re: [perl #76622] Regex recursion and backreference: 11 !~ /(^)(|1(?2)\1)$/

Thread Previous
From:
Zsbán Ambrus
Date:
November 21, 2010 04:57
Subject:
Re: [perl #76622] Regex recursion and backreference: 11 !~ /(^)(|1(?2)\1)$/
Message ID:
AANLkTi=HVq_K2u6Xq6r_QfoXoZL82FK078KVM4PZTL0i@mail.gmail.com
It turns out that this is not a bug.

Perl behaves correctly in this case.  The explanation is that contents
of capture buffers of the topmost re are not accessible from recursed
subpatterns.  This is mentioned in perlre actually:

"(?PARNO)" ...
Capture buffers contained by the pattern will have the value as
determined by the outermost recursion.

Thus, when the regex engine recurses to subpattern 2, capture 1 has
never matched, and thus a backreference to it fails no matter what.

You can save the obfu the following way, which works in at least perl
5.10.0, 5.10.1, 5.12.0, 5.12.1, 5.13.6:

perl -le'$==0,(1x$_)=~
/^(|()1(?1)(?1)\2)$(?{$=++})^/,
print$=for 0..13'

Please mark the ticket as not a bug, and sorry for the bother,

Ambrus

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