in reply to Re^2: ZERO_LENGTH match
in thread ZERO_LENGTH match



ok, the first paragraph is wrong - if you backtrack
you _can_ use the second alternative but fact is that
you'll try matching (?=c) at position 0 not at some
position after the last 'a' in the string which is the
case in /( (?: a | (?=c) ) )*/x.
The breaking of the infinite loop only forces (?=c) to
be tried only once but doesn't redefine the position
at which this happens.

Replies are listed 'Best First'.
Re^4: ZERO_LENGTH match
by fro (Novice) on Aug 02, 2005 at 08:09 UTC
    Try this:

    #!/usr/bin/perl use re "eval"; # (?{ CODE }) is a classical zero-width assertion # that allways succeeds and it is used only for its # side-effects. my $non_zero_width = 'a(?{ print 1 })'; my $zero_width = '(?{ print 2 })'; # These two should be equivalent according to perlre. my $re1 = qq/ (?: $non_zero_width | $zero_width )* /; my $re2 = qq/ (?: $non_zero_width )* | (?: $zero_width )? /; # But are they really? $_ = 'aaabbb'; print "\n-----------------\n"; /$re1/x; print "\n-----------------\n"; /$re2/x; print "\n-----------------\n";
    The output is:
    -----------------
    1112
    -----------------
    111
    -----------------
    which proves my point.