in reply to greedy search

If you think about it, there are only two reasonable choices for this: minimalist and maximalist (aka greedy) matching. In Perl (and every other regex syntax I know of) the default is to make the matching greedy; furthermore, Perl gives you the option to make the matching minimalist if that's what you want. As far as I can tell, this choice is arbitrary; it could have gone the other way.

But I'm just guessing here, and I could easily be wrong (i.e. there may be some fundamental reason for which maximalist matching is a more reasonable default than minimalist matching); if so, I look forward to being corrected.

the lowliest monk

Replies are listed 'Best First'.
Re^2: greedy search
by ikegami (Patriarch) on Jun 03, 2005 at 18:57 UTC

    There's often no difference between minimalistic and greedy matching, because of anchoring:

    /a*b/
    is the same as
    /a*?b/

    When there is a difference, minimalistic is usually just a shortcut for negative matching:

    '<a href="">text</a>' =~ /<((?!>).)*>/
    can be written as
    '<a href="">text</a>' =~ /<[^>]*>/
    can be written as
    '<a href="">text</a>' =~ /<.*?>/

    '{begin} foo {cmd} bar {end} {begin} baz {end}' =~ /{begin}((?!{end}).)*{end}/
    can be written as
    '{begin} foo {cmd} bar {end} {begin} baz {end}' =~ /{begin}.*?{end}/</code>

    While minimalistic matching can replace negative matching, it's not the same. However, I can't think of case where minimalistic matching is required. Since it's optional, many regexp engine do not provide it, making greedy the default (and only) option. (As an aside, most regexp engine usually don't provide (?!...) either, so they have a limitation.)