Repeat That Can Match Empty

Identifier: regex.lint.quantifier.emptyRepeat

When it triggers: An unbounded quantifier (*, +, {n,}) repeats an item that can match the empty string: (a*)*, (?:a|b?)+, (?:a{0,2})+. PCRE ends the loop on an empty iteration, which matches nothing more. When every way the item matches the empty string goes through a capturing group, that last, empty iteration sets the capture to the empty string, and the message says so; in (?:(a)|b?)* the empty way goes around the group, and $m[1] keeps its a, and a capture inside a lookaround keeps what the lookaround read: (?:x|(?=(a)))* on xa captures a. An item that fails rather than match the empty string is sound: (?:a|(*FAIL))* and (?:a|(?!))* are not reported. Where another rule already reports the same repeat (an empty alternative, a quantified lookaround, a nested quantifier), this one stays silent, as long as the configuration turns that other rule on: (?:a*)*b with quantifier.nested off is reported by this rule. The hint writes the repeat again, a* for (?:a*)+, except in a pattern where a call runs a repeat whose count may vary, a+ or a{2,3} but not a{2} nor a repeated call, in the group it calls or anywhere when the pattern calls itself whole: PCRE2 compiles such a pattern by how all of it is written, and its JIT then misses matches its interpreter finds. /((?:a*)+b)c(?1)d/ finds abcabd in abcabcabd with the JIT, where /(a*b)c(?1)d/ finds nothing, from PCRE2 10.39 to 10.49.

Example:

// WARNING: the capture ends empty
preg_match('/(a*)*/', 'aaa', $m);  // $m is ["aaa", ""]

// PREFERRED
preg_match('/(a*)/', 'aaa', $m);   // $m is ["aaa", "aaa"]

Fix: Make the repeated item read at least one character, or drop the outer quantifier. The tip gives a rewrite only where one matches the same text: a* for (?:a*)+, (?:a?)* or (?:a{0,2})+. Elsewhere, as for (?:a|b?)+ (which is (?:a|b)*) or (?:a{0})+ (which reads nothing), the issue comes without a tip.


Try it. Open /(a*)*/ in the playground: the finding is in the Diagnostics tab.

Edit on GitHub Feedback