develooper Front page | perl.perl6.language.regex | Postings from December 2000

Re: Perl 5's "non-greedy" matching can be TOO greedy!

Bart Lateur
December 16, 2000 17:58
Re: Perl 5's "non-greedy" matching can be TOO greedy!
Message ID:
On Fri, 15 Dec 2000 13:42:44 -0700, Kevin Walker wrote:

>Deven seems to be advocating thinking about regular expressions 
>without worrying too much about the implementation, even at a fairly 
>abstract level.

Here's a counter example:


Shouldn't a non-greedy matcher /b.*?d/, according to the OP's rules,
match "bcd", the second matching string? That is shorter than the first
match. Oh, you want the first match. Well: same thing.

It is similar in nature to that question in comp.lang.perl.misc the
other day: why doesn't greediness force /a|ab/ to preferably match "ab"?
Because it doesn't. Try from left to right, first match wins. A dead
simple rule.

If Perl's greediness/nongreediness doesn't work for you, rely on
something else, to get what you want.

	Bart. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About