Empty Group

Identifier: regex.lint.group.empty

When it triggers: A non-capturing or atomic group is empty, (?:) or (?>): it matches the empty string and changes nothing. An empty capturing group () is a placeholder that numbers a group and is left alone, as are empty lookarounds, which have rules of their own. So is a (?:) that keeps an escape apart from the digit after it, the form the library’s own printer writes: (a)\1(?:)0 is not (a)\10, and \01(?:)2 is not the newline \012; one that keeps braces from a count, a{(?:)2}, a{1,(?:)2} or a{(?:),2} ({,2} is a quantifier since PCRE2 10.43); and one a quantifier repeats: a(?:)? matches a alone, a? the empty string too. An unbounded repeat of an empty group, (?>)+, is regex.lint.quantifier.emptyRepeat’s. So is an empty group PCRE2 enters to choose which repeats it makes possessive: /a+(?>(?:)|b)a/ finds aa, where /a+(?>|b)a/ finds nothing. The engine enters it inside an atomic group, or a possessive repeat of a group, that may match nothing, on its way from a repeated character, no further than the first character that must be there: (?:) is reported in /a+x(?>(?:)|b)a/, and in /a+(?:)(?>(?:b)?)a/, outside the atomic group. A repeat that runs lazy, which PCRE2 leaves as written, counts for nothing: (?:) is reported in /a+?(?>(?:)|b)a/, and stays in /a+?(?>(?:)|b)a/U. Every empty group stays 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: with the JIT, /(a+(?:)c)x(?1)d/ finds acxacd in acxacxacd, /(a+c)x(?1)d/ nothing, and /(?>)(a+b)c(?1)d/ finds abcabd in abcabcabd from PCRE2 10.44, /(a+b)c(?1)d/ nothing. For a target before PCRE2 10.44 every empty group also stays in a pattern that holds an atomic group a branch of which, after the first, may match nothing, as (?:) in /(?>b*+..+|(?:))b/: the JIT of 10.39 to 10.42, which PHP 8.1 to 8.3 bundle, enters that branch again once an earlier one matched with a repeated character in it, and /(?>b*+..+|)b/ finds b in bbaa with it, where its interpreter finds nothing. Every empty group stays, too, in a pattern where a group repeated no time holds, in an alternative after its first, a repeat from zero without bound, an anchor, an assertion or a lookahead: PCRE2 reads that alternative as the item the pattern opens with once it has passed over what stands before, and /(?:)(?:x|.*){0}a/ finds a in ba, with the JIT and without, where /(?:x|.*){0}a/ finds nothing.

Example:

// WARNING: the group does nothing
preg_match('/a(?:)b/', 'ab');  // 1

// PREFERRED
preg_match('/ab/', 'ab');      // 1

Fix: Remove the group.


Try it. Open /a(?:)b/ in the playground: the finding is in the Diagnostics tab.

Edit on GitHub Feedback