Empty Alternatives

Identifier: regex.lint.alternation.empty

When it triggers: An alternation contains an empty branch (e.g., trailing | or ||). The hint offers a quantifier instead, 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)?a)(a++b)c(?1)d/ finds aabcaabd in aabcaabcaabd with the JIT, where /(?:a*(?:b|)a)(a++b)c(?1)d/ finds nothing, from PCRE2 10.39 to 10.49.

Example:

// WARNING: Empty alternative
preg_match('/a|/', $input);

// PREFERRED: Use a quantifier
preg_match('/a?/', $input);

Fix: Replace the empty alternative with a quantifier or an explicit empty group if intentional.


Try it. Open /a|/ in the playground: the finding is in the Diagnostics tab.

Edit on GitHub Feedback