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
brian d foy
December 15, 2000 09:36
Re: Perl 5's "non-greedy" matching can be TOO greedy!
Message ID:
On Thu, 14 Dec 2000, Deven T. Corzine wrote:

> Here is a simple example: (tested with perl 5.005_03)
>      $_ = "aaaabbbbccccddddeeee";
>      ($greedy) = /(b.*d)/;              # "bbbbccccdddd" (correct)
>      ($non_greedy) = /(b.*?d)/;         # "bbbbccccd" (should be "bccccd"!)

> Does anyone disagree with the premise, and believe that "bbbbccccd" is the
> CORRECT match for the non-greedy regexp above?

the quantifier applies to the .* portion. that part finds its shortest
possible match.  the regex engine, however, choose the first "b" it
encountered as it's start point.  this is unrelated to the behaviour of ?.
you don't like that it finds the earliest match, instead of, say, the
longest match or the shortest match, or even all the possible matches
(hmmm... module idea ;).

perhaps, however, Perl 6 wil allow you to easily use your own custom regex
engine and then you can make it do whatever you like. ;)

brian d foy                              <>
Director of Technology, Smith Renaud, Inc.
875 Avenue of the Americas, 2510, New York, NY  10001
	V: (212) 239-8985

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