-
What language is this for?
Java 17 -
Which rule?
S5860: “Use the named groups of this regex or remove the names.” -
Why do you believe it’s a false-positive/false-negative?
Because my code is using the group name to fetch the data in the group -
Are you using
- SonarQube - which version?
Enterprise Edition Version 9.8 (build 63668)
- SonarQube - which version?
-
How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
Below is an example representation of my code which throws the error, highlighting the group in the Pattern definition.
I’ve also tried moving the static field inside the function, and simplifying the regex pattern down to just the group itself, and the issue keeps showing up. The only way I’ve managed to make the issue disappear is by removing the group name from the regex pattern.
Example:
package org.my_package;
import java.util.regex.Pattern;
public class Example {
private static final Pattern MY_PARSER = Pattern.compile(
"(?i)<form .*?name ?= ?\"xyz\" .*?action ?= ?\"(?<action>.*?)\"");
public static String getAction(final String test) {
var matcher = MY_PARSER.matcher(test);
String output = "";
if (matcher.find()) {
output = matcher.group("action");
}
return output;
}
}