Re: Perl 5's "non-greedy" matching can be TOO greedy!
[email protected] (Jeff Pinyan) Thu, 14 Dec 2000 20:51:26 -0500 (EST)
| Newsgroups | perl.perl6.language.regex |
|---|---|
| Message-ID | <[email protected]> |
On Dec 14, Randal L. Schwartz said: >Deven> I haven't even SEEN an example where the current behavior is >Deven> actually preferable than my proposed behavior, have you? (And >Deven> I'd expect at least a FEW, though I suspect there are probably >Deven> more counterexamples.) > >If I want the leftmost B that is followed by the shortest number >of characters to get to a D, I write /B.*?D/. The "leftmost" part >keeps getting left out of regex discussions, and maybe that's why >you're thrown off. But it's a pretty consistent overriding factor. > >If you want something odd like "not necessarily the leftmost", then >you'll need to speak more. But "leftmost" is fundamental to the >design of regex. Don't mess with that. Or don't call it a regex any >more. You could use my sexeger technique to get this behavior (possibly): $string = "aaabbbcccdddeee"; # regex to be reversed: /b(.*?)d/ $revstr = reverse $string; ($match) = $revstr =~ /d(.*?)b/; No, that doesn't quite work. It works when you unroll the .*? loop: # regex to be reversed: /b([^bd]*)d/ ($match) = $revstr =~ /d([^bd]*)b/; But then why would you use sexeger at all, when you can unroll the loop in the forward direction? ($match) = $string =~ /b([^bd]*)d/; -- Jeff "japhy" Pinyan [email protected] 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/