in reply to Re: question about the star(*) quantifier
in thread question about the star(*) quantifier

Hi Sam,
Perl's regex engine finds the left-most match

I would have phrased it like this: like most engines Perl's looks by default for the longest leftmost match.

% stephan@labaule (/home/stephan) % % echo "okay stephan do test it!" | + > perl -lne 'm!(steph.?)!; print "seen: [$1]\n"' seen: [stepha]

Actually POSIX mandates longest possible for alternations as they all start at the same place. The owl book calls that POSIX NFA.

% stephan@labaule (/home/stephan) % % echo "okay stephan do test it!" | > perl -lne 'm!(steph|steph.?)!; print "seen: [$1]\n"' seen: [steph]

so yes, perl's is not POSIX NFA (for various efficiency reasons)

Actually not many engines follow POSIX on this, mostly (ugly -- because too ancient) system libraries (on Un*x), ksh's does too, even the Hackerlab library does (on second thought seems normal as it was supposed to be a drop-in replacement for the C lib on Un*x -- and POSIX). Tcl's is hybrid so I am not sure. And every engine after Perl5 birth has essentially copied Perl's.

regards --stephan
by the way ksh notation is interesting (zsh can masquerade as ksh for this if you don't have a ksh to try)

Replies are listed 'Best First'.
Re^3: question about the star(*) quantifier
by samtregar (Abbot) on Dec 21, 2006 at 20:51 UTC
    I first wrote "longest left-most" and then revised it to just "left-most" when I remembered that Perl stops at the first matching alternation, longest or not. Maybe it's useful to think of it as "longest" anyway, but I prefer "greedy".

    -sam