in reply to The "anchor" misnomer in regexes

The matching isn't minimal; there's an obvious smaller match which satisfies the criteria of "hash mark", "as little as possible", "end of string", which perl does not find for this regexp.

This doesn't satisfy the meaning of "minimal match" in English, so of course it's confusing.

If the "left-most-longest" principle must be used here, then the intent of the minimal match has been overpowered by a greedy rule. For minimal matches, the "left-most-longest" should be replaced by "left-most-shortest". If it's not, then "mimimal matching" isn't minimal at all!

The problem with regular expressions is that they attempt to be a set of rules for what gets matched, without a documented set of rules for how that matching gets done. When implicit rules about how matching gets done (ie. minimal matching isn't) raise their ugly head, of course people get confused; they've been told the details don't matter, when ultimately they really do.

Replies are listed 'Best First'.
Re: "Minimal" matching isn't....
by Eimi Metamorphoumai (Deacon) on Dec 19, 2005 at 21:13 UTC
    I think part of the problem is the term "minimal" which gets used. "Greedy" is a good term for what * does normally--grab as much as possible and only give it up if you really have to. But the opposite behavior, the one for *? isn't minimal, it's lazy. Take as little as you possibly can, and move on. If you're backtracked into, then take another character. It's not optimizing for the smallest match, just for the first one it can find given its laziness.
Re: “Minimal” matching isn’t…
by Aristotle (Chancellor) on Dec 26, 2005 at 06:04 UTC

    For minimal matches, the “left-most-longest” should be replaced by “left-most-shortest”

    Which is exactly what the shown match is: the shortest match from the left-most possible position. “Left-most” always takes precedence over all other rules. I guess this doesn’t get emphasised enough.

    (Was that your point?)

    Makeshifts last the longest.