The replacement of preg_replace refers to a group that the pattern does not have, which PHP replaces with nothing

tx0/php:regex-replacement-missing-grouprecommendedwarningreliability/strings-and-regexdepth fastbug

What it finds

The replacement of preg_replace refers to a group that the pattern does not have, which PHP replaces with nothing

Example from the rule's tests
<?php$a = preg_replace('/(\d+)-(\d+)/', '$2.$3', $s);tx0 reports: refers to group 3, but the pattern has 2 groups$b = preg_replace('/\s+/', '\1', $s);tx0 reports: refers to group 1, but the pattern has no groups$c = preg_replace('/(\d+)-(\d+)/', '${2}.$1', $s);$d = preg_replace('/\s+/', '$0', $s);$e = preg_replace('/(a)/', 'costs $ 5', $s);$f = preg_replace('/(a)/', $replacement, $s);$g = preg_replace('/(a)/', '\\$2 costs', $s);$h = preg_replace('/(a)/', "\\\\2", $s);
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/php:regex-replacement-missing-group" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/php:regex-replacement-missing-group] reason="..."

tx0 explain tx0/php:regex-replacement-missing-group shows the same in the terminal. Found a false positive? Report it.