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:
Kevin Walker
Date:
December 15, 2000 12:03
Subject:
Re: Perl 5's "non-greedy" matching can be TOO greedy!
Message ID:
v04210100b66025df1998@[207.170.238.104]
"Deven T. Corzine" <deven@ties.org> writes:

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

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?


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