PREV UP NEXT Regex

Chapter 6: What Gets Matched?

Regex usually matches strings according to the ``leftmost longest'' rule; that is, it chooses the longest of the leftmost matches. This does not mean that for a regular expression containing subexpressions that it simply chooses the longest match for each subexpression, left to right; the overall match must also be the longest possible one.

For example, (ac*)(c*d[ac]*)\1 matches acdacaaa, not acdac, as it would if it were to choose the longest match for the first subexpression.