Quantifier does not follow a repeatable item
A quantifier follows an anchor, and an anchor matches no character: there is nothing to repeat.
preg_match('/\b+a/', $value);
// preg_match(): Compilation failed: quantifier does not follow a repeatable item at offset 3
\b is a word boundary. It holds no character for + to repeat, so PCRE2 refuses the pattern. The offset is that of the quantifier. preg_replace() and the other functions print the same message under their own name.
Why PHP prints it
PCRE2 repeats the item that stands before a quantifier. An item that matches no character, or no item at all, cannot be repeated.
An anchor, an assertion or a verb.
preg_match('/^*/', $value);
// preg_match(): Compilation failed: quantifier does not follow a repeatable item at offset 2
^, $, \b and \B match a position, and (*FAIL) and \K match no character either. /a$+/ and /\K+/ are refused the same way.
A branch that starts with the quantifier.
preg_match('/a|*b/', $value);
// preg_match(): Compilation failed: quantifier does not follow a repeatable item at offset 3
After | the branch is empty. A quantifier in first position, as in /*/, gives the same message.
Another quantifier.
preg_match('/a*?+b/', $value);
// preg_match(): Compilation failed: quantifier does not follow a repeatable item at offset 4
A ? or a + after a quantifier makes it lazy or possessive: a*? and a*+ are valid. A third character has nothing to repeat.
A lookaround is a group, so it takes a quantifier: /(?=a)*/ compiles.
How to fix it
Put the quantifier after the item it repeats, or escape it to match the character itself.
// The quantifier after the letter: a word boundary, then a run of a
preg_match('/\ba+/', 'abc');
// matches "a"
// A literal *, escaped
preg_match('/^\*/', '*note');
// matches "*"
// A lazy quantifier, then a literal +
preg_match('/a*?\+b/', 'aa+b');
// matches "aa+b"
// The alternatives grouped, then repeated
preg_match('/^(?:a|b)*$/', 'abba');
// matches "abba"
For /a*?+b/, drop the ? if the possessive a*+ was meant.
Where it hides
- A quantifier character meant as text.
/^* /was written for a Markdown bullet, and PHP refuses it at offset 2. Write/^\* /. - A generated pattern.
'/^' . $prefix . '+/'reads/^+/when$prefixis empty. - An inline flag before the quantifier.
/(?i)*/prints the message at offset 5: the flags are not an item to repeat. - One quantifier character too many.
/a**/and/a{1,2}{2}/print the message. - Another language’s habit. Perl accepts
\b+. PCRE2 refuses it.
What PHPRegex reports
regex.quantifier.nothingToRepeatfor/\b+a/:Quantifier "+" cannot be applied to assertion or verb "\b" at position 3.With no item before the quantifier, as in/a|*b/, the message isQuantifier without target at position 3.Both come with the hint:Put the quantifier after the item it repeats, or escape it with a backslash to match it literally.
Open /\b+a/ in the playground: the Diagnostics tab shows the code, its message, and what PHP says.
Related
- Chapter 4: Quantifiers & Greediness: greedy, lazy and counted repeats.
- Chapter 3: Anchors & Boundaries:
^,$and\b. - Quantified assertion: a lookaround that carries a quantifier constrains nothing.
- Numbers out of order in {} quantifier: when the two counts of a
{…}repeat are swapped.