Quantified Assertion

Identifier: regex.lint.quantifier.assertion

When it triggers: A lookaround carries a quantifier. With a minimum of zero PCRE tries the rest of the pattern with and without the assertion, so it constrains nothing; with a minimum of one or more, PCRE checks it once. A lookaround that captures is left alone: (?=(\w+))? still sets its group when it holds. So is one a match may skip 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+(?=a)?b)c(?1)d/ and /(?=a)?(a+b)c(?1)d/ find abcabd in abcabcabd with the JIT, where /(a+b)c(?1)d/ finds nothing, from PCRE2 10.39 to 10.49.

Example:

// WARNING: the lookahead may be skipped
preg_match('/(?=\d)?\w/', 'a');  // 1

// PREFERRED
preg_match('/(?=\d)\w/', 'a');   // 0

Fix: Remove the quantifier, or the assertion.


Try it. Open /(?=\d)?\w/ in the playground: the finding is in the Diagnostics tab.

Edit on GitHub Feedback