S5860 - Names of regular expressions named groups should be used (when they're actually...)


An unused named group is reported while it is effectively used.


  • Eclipse IDE
    Version: 2020-12 (4.18.0)
    Build id: 20201210-1552

  • SonarLint

Sample Code

import java.util.regex.Pattern;

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("\\u005C(?<escapableChars>[\\u005C.])");

    pattern.matcher("\\\\.case").replaceAll("${escapableChars}"); // returns "\\.case"

An info raises saying escapableChars named group isn’t used. However, it is involved in the Matcher#replaceAll call.

Hi Stephan,

Thanks for this feedback and the code reproducer that help me to confirm that it’s a false-positive.
I’ve created this ticket SONARJAVA-3688 to fix it.

Good luck writing regular expressions :slightly_smiling_face:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.