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

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

Kevin Walker
December 15, 2000 12:03
Re: Perl 5's "non-greedy" matching can be TOO greedy!
Message ID:
"Deven T. Corzine" <> writes:

>I've yet to see a concrete example of where the current behavior is

What about matching C comments?

   ($first_comment) = $code =~ m!(/\*.*?\*/)!s;
           # (ignore issues with quoted strings in $code

Works correctly under the current behavior.  It would break under you 
proposal (if I understand your proposal correctly).

More generally, it seems to me that you're hung up on the description 
of "*?" as "shortest possible match".  That's an ambiguous 
simplification of what "*?" means.  It might better be described as 
"match until you find a match for the rest of the regex" ('d' in your 
example).  If oversimplifications in the documentation led you to 
believe that "*?" meant something it was never intended to mean, then 
perhaps the documentation should be clarified.  But the meaning of 
"*?" should not be changed.

Final point:  In your example, /b[^bd]*d/ does what you want, and is 
arguably easier to understand.  Can you think of any examples where 
it's not this easy to get the behavior you desire? Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About