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

[email protected] (Kevin Walker) Fri, 15 Dec 2000 13:01:39 -0700
Newsgroups perl.perl6.language.regex
Message-ID <v04210100b66025df1998@[207.170.238.104]>
"Deven T. Corzine" <[email protected]> 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?