develooper Front page | perl.perl5.porters | Postings from July 2018

Re: [perl #133352] Ancient Regex Regression

Thread Previous | Thread Next
From:
David Nicol
Date:
July 18, 2018 18:01
Subject:
Re: [perl #133352] Ancient Regex Regression
Message ID:
CAFwScO8LrYJSkSA41wqmFUiEgcYKkrQzsVRjUxZSQD7-LwBLjg@mail.gmail.com
> >
> > Personally, invalidating a successful capture that was part of a
> > successful match feels wrong to me.  Consider this example:
> >
> >      "foobar" =~ /^ (?: (foo) | (bar) )* $/x;
> >
> > Both with and without my patch, this leaves $1="foo" and $2="bar".  If
> > the "unused" capture were to be invalidated, this would leave $1
> > undefined instead.  Would this be desirable?
>
> It would match the (admittedly ambiguous) bit in perlre that David Nicol
> quoted:
>
>     If a group did not match, the associated backreference won't match
>     either. (This can happen if the group is optional, or in a different
>     branch of an alternation.)
>


the expression in the capture matching isn't enough; the capture has to
match and then the outer expression has to match too.
I'd like to think that this can be fixed by rearranging the order of things
so the assignment to the capture group is deferred until
the branch the capture group is in matches.

"go" matches $1 but does not clobber $1 because "gox" is neither [fg]oo nor
bar
$ perl -le '"foobargox" =~ /^ (?: ([fg]o)(o) | (bar) )* /x; print
"1:$1;2:$2;3:$3"'
1:fo;2:o;3:bar


"go" clobbers $1 even though "gox" is not [fg]oo, as a later alternative
within this iteration matches
$ perl -le '"foobargox" =~ /^ (?: ([fg]o)(o) | gox | (bar) )* /x; print
"1:$1;2:$2;3:$3"'
1:go;2:o;3:bar


When exactly does the assignment happen?



-- 
"At this point, given the limited available data, certainty about only a
very small number of things can be achieved." -- Plato, and others

Thread Previous | Thread Next


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