Impossible Lookaround

Identifier: regex.lint.lookaround.impossible

When it triggers: A lookahead contradicts what the pattern reads after it: (?=a)b asks for an a where the pattern reads a b, (?!a)a forbids the a it reads. The automata decide it: what follows the lookahead runs through the enclosing groups and quantifiers (/(?:x(?=a))+b/ is reported: another x or the b comes next, never an a), read for a few items. A contradiction in one alternative is reported though the pattern still matches through another (/(?:x(?=a)|y)b/). The rule stays silent where it cannot read: a backreference, a lookbehind or \K after the lookahead, a group a subroutine call runs from elsewhere, a pattern past the automata’s work cap or past the eighth lookahead it asks them about, and in UTF mode a word boundary, \w or a Unicode property near the lookahead. Without /u, a byte read caselessly has the case the target’s PCRE2 gives it: under (*UCP) the case of the character of the same value from 10.35, the tables’ before, so /(*UCP)(?!\xC9)\xE9/i is reported for PHP 8.0 and later and not for PHP 7.4 with PCRE2 10.34, which matches it on \xE9. Under another locale’s tables the rule stays silent where the engine’s paths read a byte apart: under macOS’s tr_TR.ISO8859-9 the JIT matches "/(?=\xFD)i/i" on I and the interpreter does not, and the interpreter alone matches /(?![\xFDI])I/ on I, as PCRE2 reads a class of a byte and its other case as the byte, caseless. Under /u an ASCII letter written alone, i, \x69, [i] or [^i], has the case the tables of the target’s locale give it, as PCRE2 compares it, where a class of several characters has Unicode’s: under glibc’s tr_TR.UTF-8, which gives i and I no other case, /(?!i)I/iu matches I and is not reported, and /(?![^i])I/iu, whose class takes I there, is. Under (*TURKISH_CASING), which PCRE2 reads from 10.45, i and I read caselessly pair with İ and ı and not with each other: /(*TURKISH_CASING)(?!i)I/iu matches I and is not reported, /(*TURKISH_CASING)(?=i)I/iu is. A repeated lookahead is regex.lint.quantifier.assertion’s, and an empty negative lookahead (?!) fails on purpose. The rule stays silent in a pattern that sets an ASCII option ((?a), (?aD), …) or (?xx), or sets r beside a (?^): the flags it reads do not say which of them is in force.

Example:

// WARNING: a digit is required where a letter is read
preg_match('/(?=\d)[a-z]+/', 'a1');  // 0

// WARNING: the "a" is forbidden, then read
preg_match('/(?!a)a/', 'aa');         // 0

Fix: Fix the lookahead or what follows it, or remove the alternative that cannot match.


Try it. Open /(?=\d)[a-z]+/ in the playground: the finding is in the Diagnostics tab.

Edit on GitHub Feedback