Lookbehind assertion is not fixed length

The lookbehind holds a quantifier without a ceiling, and PCRE2 refuses to compile it.

preg_match('/(?<=a+)b/', $value);
// preg_match(): Compilation failed: length of lookbehind assertion is not limited at offset 0

A lookbehind needs a maximum length: PCRE2 must know how far back the check can reach, and a+ has none. The offset, counted from the opening delimiter, is the ( that opens the lookbehind.

The wording depends on the release. PHP 8.4 and later print this one. Before, on a PCRE2 before 10.43, the same pattern gives lookbehind assertion is not fixed length at offset 0.

Why PHP prints it

A quantifier without a ceiling inside a lookbehind gives it no maximum length. +, * and {n,} are the three. Every lookbehind of the pattern is checked, positive or negative.

+, * or {n,} in the lookbehind.

preg_match('/(?<=a{1,})b/', $value);
// preg_match(): Compilation failed: length of lookbehind assertion is not limited at offset 0

A negative lookbehind. It is refused the same way:

preg_match('/(?<!a+)b/', $value);
// preg_match(): Compilation failed: length of lookbehind assertion is not limited at offset 0

One alternative without a ceiling. The others may be fixed:

preg_match('/(?<=a+|b)c/', $value);
// preg_match(): Compilation failed: length of lookbehind assertion is not limited at offset 0

How to fix it

Three fixes need no bound:

// Preceded by one or more a is preceded by a: the quantifier goes
preg_match('/(?<=a)b/', $value); // matches "b" in "aaab"

// Or capture the text instead of looking at it
preg_match('/(a+)b/', $value, $matches); // matches "aaab", and $matches[1] holds "aaa"

// Or match the text, then restart the match at b
preg_match('/a+\Kb/', $value, $matches); // matches "b"

(?<=a)b says what (?<=a+)b meant: the text before b ends with a+ exactly when it ends with a. The capture puts the as in $matches[0]. With \K, $matches[0] is b as with a lookbehind, but the as are consumed: /^a+\Kb$/ matches aaab, where /^(?<=a)b$/ matches nothing.

The fourth fix gives the quantifier a ceiling, and works from PHP 8.4: (?<=a{1,3})b is accepted from PHP 8.4 and refused before, while (?<=a|bb), two alternatives of different lengths, is accepted on every PHP 8.

// A ceiling, from PHP 8.4
preg_match('/(?<=a{1,3})b/', $value); // matches "b" in "aaab"

The ceiling has one of its own, at 255 characters. /(?<=a{1,255})b/ compiles, and /(?<=a{1,256})b/ is refused with another message:

preg_match('/(?<=a{1,256})b/', $value);
// preg_match(): Compilation failed: branch too long in variable-length lookbehind assertion at offset 0

Where it hides

  • A lookbehind written for JavaScript. JavaScript accepts /(?<=a+)b/, and matches b in aaab.
  • A quantifier deeper in the lookbehind. /(?<=\w+)b/ and /(?<=ab+)b/ give the same message.
  • A count without a ceiling. /(?<=\d{3,})b/: {3,} is refused like +.
  • A negative lookbehind. /(?<!a+)b/ asks for what /(?<!a)b/ says: not preceded by one a is not preceded by a+.

What PHPRegex reports

  • regex.lookbehind.unbounded for /(?<=a+)b/: “Lookbehind is unbounded. PCRE requires a bounded maximum length.” The library adds the offset and a hint: “Use a bounded quantifier instead of “+”.”
  • regex.lookbehind.variableLengthNotSupported for /(?<=a{1,3})b/ when the pattern is checked for a PHP before 8.4, with php_version set to 8.3: Variable-length lookbehind needs PCRE2 10.43, which PHP bundles from 8.4. The hint follows: Give each branch of the lookbehind a fixed length, or target PHP 8.4+.

Open /(?<=a+)b/ in the playground: the Diagnostics tab shows the code, its message, and what PHP says.

Edit on GitHub Feedback