jeffatrobertsdotnet has asked for the wisdom of the Perl Monks concerning the following question:
I expected (hoped) that the first RE would fail - but it does not. It seems the negative lookahead causes a "lazy" match on the 1st pattern. That is, $& is set to 'aa' as if it were non-greedy. If I change the {2,} quantifier to {3,} the pattern works as I expect. That is, the pattern match fails. If I change the pattern to {2,3} is behaves the same as {2,}.$_='aaab'; print "Matched 1=>$_\n" if /^[a]{2,}(?![b])+/; print "LastMatch1:$&\n"; print "Matched 2=>$_\n" if /^[a]{2,}/; print "LastMatch2:$&\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Greedy match and zero-width negative lookahead assertion
by Eily (Monsignor) on Mar 16, 2018 at 16:16 UTC | |
by jeffatrobertsdotnet (Initiate) on Mar 16, 2018 at 16:52 UTC | |
|
Re: Greedy match and zero-width negative lookahead assertion
by AnomalousMonk (Archbishop) on Mar 16, 2018 at 20:27 UTC |