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

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

Thread Previous | Thread Next
From:
Bart Lateur
Date:
December 16, 2000 17:58
Subject:
Re: Perl 5's "non-greedy" matching can be TOO greedy!
Message ID:
9a7o3t8bqbu00oc42pl2io74fdeevdcb4s@4ax.com
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:

	/aaaabbbbccccdddddbbbbcdddd/

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.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About