False positive for S5860 when using named group regex

  • 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)
  • 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.


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;            

Hi Shawn, and welcome to the community!

Thanks for your report. However, I’m unable to reproduce the issue. Creating a new project and using your example one to one, I get:

  • no reported issue with the code as-is
  • an issue reported for S5860 for the action group, if I comment out the line output = matcher.group("action"); as expected.

Tested with: SonarQube Version 9.9 (build 65466)

Can you rule out that secondary effects might be the reason, such as other errors in your original file that might prevent the analyzer from analyzing it entirely?

If the problem still remains, I’d suggest trying out SonarQube Version 9.9.

No other errors, I’ll see if we can update to 9.9 and will try it again and report back.
Thanks for the reply!