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

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

From:
Jeff Pinyan
Date:
December 14, 2000 14:54
Subject:
Re: Perl 5's "non-greedy" matching can be TOO greedy!
Message ID:
Pine.GSO.4.21.0012141753130.4213-100000@crusoe.crusoe.net
On Dec 14, Deven T. Corzine said:

>> You're asking for something like
>> 
>>   /(?<!b)(b.*?d)/
>> 
>> which is an "optimization" you'll have to incorporate on your own.
>
>Thanks for the example.  Unfortunately, your attempted workaround doesn't
>even work for the example string; the "a" preceding "bbbbccccd" isn't a
>"b", so the regexp engine is still perfectly happy with the same match.
>Even if it worked as you intended, it would have failed with something like
>"bbbabbbccccdddd", since the ".*?" would happily match "bbabbbcccc"...

Sorry, I was thinking backwards (when I was supposed to think forwards).

Using:

  /(b(?!b).*?d)/

is what I'd meant to say.

I wouldn't call the current behavior a design flaw, though.

-- 
Jeff "japhy" Pinyan     japhy@pobox.com    http://www.pobox.com/~japhy/
CPAN - #1 Perl Resource  (my id:  PINYAN)       http://search.cpan.org/
PerlMonks - An Online Perl Community          http://www.perlmonks.com/
The Perl Archive - Articles, Forums, etc.   http://www.perlarchive.com/




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