Export (0) Print
Expand All

Quantifiers and Empty Matches 

Quantifiers *, +, {n,m} (and their "lazy" counterparts) never repeat after an empty match when the minimum number n has been matched. This rule prevents quantifiers from entering infinite loops on empty matches when m is infinite (although the rule applies even if m is not infinite).

For example, (a?)* matches the string "aaa" and captures substrings in the pattern (a)(a)(a)(). Note that there is no fifth empty capture, because the fourth empty capture causes the quantifier to stop repeating.

Similarly, (a\1|(?(1)\1)){0,2} matches the empty string rather than "a" because it never tries the expansion ()(a). The {0,2} quantifier allows only empty matches in the last iteration. In contrast, (a\1|(?(1)\1)){2} actually matches "a" because it does try ()(a); the minimum number of iterations, 2, forces the engine to repeat after an empty match.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft