develooper Front page | perl.perl5.porters | Postings from June 2016

Re: [perl #128215] Regex weirdness - capture group not reset

Thread Previous
Dave Mitchell
June 21, 2016 15:21
Re: [perl #128215] Regex weirdness - capture group not reset
Message ID:
On Mon, May 23, 2016 at 04:35:58AM -0700, Zsban Ambrus via RT wrote:
> On Sun May 22 16:15:17 2016, wrote:
> > The following regex matches the string abA:
> > 
> > perl -lwe 'print for shift =~ /^(([ab])|([ab]))*(\3)$/i' abA
> > b
> > b
> > a
> > A
> That looks correct to me.  The regex can match only one way: the first
> character is matched by the right hand side alternative, the second
> character is matched by the left hand side alternative, and the third
> character is matched by the backreference.  As a result, $2 is set from
> the first character because the left alternative doesn't match later; $1
> and $3 are both match from the second character because that's the last
> time those groups matched; and $4 is set from the last character.

Yes, successful captures are intentionally kept across subsequent
iterations of quantifiers, until overwritten.

e.g. this:

"abcde" =~ /(?: (?: ([acd]) | ([bde]) ) (?{ print "1=[$1] 2=[$2]\n" }) )+/x;


    1=[a] 2=[]
    1=[a] 2=[b]
    1=[c] 2=[b]
    1=[d] 2=[b]
    1=[d] 2=[e]


Wesley Crusher gets beaten up by his classmates for being a smarmy git,
and consequently has a go at making some friends of his own age for a
    -- Things That Never Happen in "Star Trek" #18

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About