Redundant Non-Capturing Group
Identifier: regex.lint.group.redundant
When it triggers: A non-capturing group wraps a single atomic token without changing precedence.
An empty group is regex.lint.group.empty’s, and a group that keeps an escape apart from
the digit after it is not redundant: (a)\1(?:0) matches aa0, (a)\10 does not, and
(?:\N){U+41} reads {U+41} as text where \N{U+41} is the code point under /u, and does
not compile without it. Neither is a group that keeps braces from reading as a quantifier, a{(?:2)} (it matches a{2},
a{2} matches aa), nor a group a quantifier repeats around anything but one character:
without the group, (?:\Qab\E)+ repeats the b alone, (?:é)+ without /u the last byte
of the letter, and (?:^)+ does not compile. (?:a)+ is reported: a+ is the same. A group
whose brackets decide which repeats PCRE2 makes possessive stays too: PCRE2 makes a character
repeated before an atomic group, or a possessive repeat of a group, that may match nothing
possessive, as if the group could not be skipped, so /a+(?:b)?+a/ finds nothing in aa
where /a+b?+a/ finds it, and /(?:a)+(?:b)?+a/ finds aa where /a+(?:b)?+a/ does not.
The engine reads those brackets on its way from the repeated character to the group it may
skip, no further than the first character that must be there: (?:b) stays in
/a+c?(?:b)?+a/, and is reported in /a+x(?:b)?+a/, as (?:\\) is in
/(?:\\)?define [a-z]++ (?:,x)*+ /. A repeat that runs lazy, which PCRE2 leaves as written,
decides nothing: (?:b) is reported in /a+?(?:b)?+a/, and stays in /a+?(?:b)?+a/U, where
U makes a+? greedy. A group repeated more than once is read again for each
round, (?:b) in /(?:(?>(?:b)?)a+){2}/ staying for the a+ of the round before, and a
pattern that holds a subroutine call is read whole: there a group stays wherever a repeat
stands before such a group. 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, a group stays wherever it stands: PCRE2 makes
such a repeat possessive by the brackets around it, its JIT then misses matches, and how the
whole pattern is written decides which. /(a+b)c(?1)d/ finds nothing in abcabcabd where
/((?:a)+b)c(?1)d/ and /(a+(?:b))c(?1)d/ find abcabd, /x(?R)a|a*/ finds the empty
string in xaa where /x(?R)a|(?:a)*/ finds xaa, and /(?:)a?(?<q>a+b)c(?&q)d/ finds
nothing in aabcaabcabd where /(?:)(?:a)?(?<q>a+b)c(?&q)d/ finds aabcabd. (?:a) is
still reported in /(?:a)+b(?1)(c)/, whose call runs no repeat. For a target before PCRE2
10.44 a group also stays in a pattern that holds an atomic group a branch of which, after
the first, may match nothing: 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, so
/(?>b*+..+|)b/ finds b in bbaa where /(?>(?:b)*+..+|)b/ finds nothing. A group stays,
with the JIT and without, 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, and
/(?:(?:x|^)){0}a/ finds a in ba where /(?:x|^){0}a/ finds nothing.
Example:
// WARNING: the group wraps one token, `a`
preg_match('/x(?:a)y/', $input);
// PREFERRED: Remove the group
preg_match('/xay/', $input);
Fix: Remove the unnecessary group.
Try it. Open /x(?:a)y/ in the playground: the finding is in the Diagnostics tab.