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

Re: /(\w|(?{++$n}))*/

Thread Previous
From:
Mark Jason Dominus
Date:
October 29, 2003 14:57
Subject:
Re: /(\w|(?{++$n}))*/
Message ID:
20031029225820.26316.qmail@plover.com

> On Oct 29, Mark Jason Dominus said:
> 
> >> >>   "abc" =~ /(b?|[ac])*/;
> >> >
> >> >Why should it match 'abc'?  The first alternative alternative b? is
> >> >taken before the seoncd alternative [ac]* .
> >> >
> >> >The * operator has special code in it to prevent it from going into an
> >> >infinite loop when its argument has matched an empty string, so the *
> >> >loop is exited after the first pass.
> >>
> >> Hrm.  I guess I was confusing that with /\w??/g.
> >
> >If it didn't have that logic in it, it would go into an infinite loop
> >in this example, since it would match an infinite sequence of b?'s at
> >the start of the target string.
> 
> I was expecting it to have the "can't match two zero-lengths in a row"
> logic, and so the b? would fail, and it would try the alternative, [ac].

For that to work, the * operator would have to know that it was
controlling an alternation, and force backtracking if the second
iteration didn't cause any progress.  It can't do that in general,
because if it did, then constructions like

        /()*/

would fail to match any string.

So instead of backtracking, the * just cuts the iteration short and
goes on to the next thing.


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