admin管理员组

文章数量:1122846

I have a function that's supposed to replace simple combinations (ex: &a &c &b &l) to something which the chat interpreter can understand and parse (<color:green> etc...)

But when working with something like &a$&e%price% I get a Illegal group reference error

I handle the price replacement first!

Here's my code:

public class Chat {

    private static final Pattern PATTERN = Patternpile("([^\\\\]?)&([0-9a-fk-r])");

    private static final Map<String, String> COLOR_CODES = new HashMap<>() {{
        put("0", "<reset><color:black>");
        put("1", "<reset><color:dark_blue>");
        put("2", "<reset><color:dark_green>");
        put("3", "<reset><color:dark_aqua>");
        put("4", "<reset><color:dark_red>");
        put("5", "<reset><color:dark_purple>");
        put("6", "<reset><color:gold>");
        put("7", "<reset><color:gray>");
        put("8", "<reset><color:dark_gray>");
        put("9", "<reset><color:blue>");
        put("a", "<reset><color:green>");
        put("b", "<reset><color:aqua>");
        put("c", "<reset><color:red>");
        put("d", "<reset><color:light_purple>");
        put("e", "<reset><color:yellow>");
        put("f", "<reset><color:white>");
        put("k", "<obfuscated>");
        put("l", "<bold>");
        put("m", "<strikethrough>");
        put("n", "<underlined>");
        put("o", "<italic>");
        put("r", "<reset>");
    }};

    public static Component translate(String text) {
        // Then apply color codes
        return Colors.interprate(replaceprimitive(text.replaceAll(String.valueOf(LegacyComponentSerializer.SECTION_CHAR), "&")));
    }

    private static String replaceprimitive(String string) {
        return PATTERN.matcher(string).replaceAll(matchResult -> matchResult.group(1) + COLOR_CODES.get(matchResult.group(2)));
    }
}

and here's how I call it:

sender.send(Chat.translate("&c$&e%test%".replaceAll("%test%", "100")));

Note this issue does not occurs if there are no simple codes in between the $ and the number example: &c$%test% doesn't cause any issues but $&l%test% does

I've tried using Matcher.quoteReplacement and even tried doing it manually and replacing the $\\$ but nothing worked.

Note: The placeholder %price% needs to be replaced before entering the function

I have a function that's supposed to replace simple combinations (ex: &a &c &b &l) to something which the chat interpreter can understand and parse (<color:green> etc...)

But when working with something like &a$&e%price% I get a Illegal group reference error

I handle the price replacement first!

Here's my code:

public class Chat {

    private static final Pattern PATTERN = Pattern.compile("([^\\\\]?)&([0-9a-fk-r])");

    private static final Map<String, String> COLOR_CODES = new HashMap<>() {{
        put("0", "<reset><color:black>");
        put("1", "<reset><color:dark_blue>");
        put("2", "<reset><color:dark_green>");
        put("3", "<reset><color:dark_aqua>");
        put("4", "<reset><color:dark_red>");
        put("5", "<reset><color:dark_purple>");
        put("6", "<reset><color:gold>");
        put("7", "<reset><color:gray>");
        put("8", "<reset><color:dark_gray>");
        put("9", "<reset><color:blue>");
        put("a", "<reset><color:green>");
        put("b", "<reset><color:aqua>");
        put("c", "<reset><color:red>");
        put("d", "<reset><color:light_purple>");
        put("e", "<reset><color:yellow>");
        put("f", "<reset><color:white>");
        put("k", "<obfuscated>");
        put("l", "<bold>");
        put("m", "<strikethrough>");
        put("n", "<underlined>");
        put("o", "<italic>");
        put("r", "<reset>");
    }};

    public static Component translate(String text) {
        // Then apply color codes
        return Colors.interprate(replaceprimitive(text.replaceAll(String.valueOf(LegacyComponentSerializer.SECTION_CHAR), "&")));
    }

    private static String replaceprimitive(String string) {
        return PATTERN.matcher(string).replaceAll(matchResult -> matchResult.group(1) + COLOR_CODES.get(matchResult.group(2)));
    }
}

and here's how I call it:

sender.send(Chat.translate("&c$&e%test%".replaceAll("%test%", "100")));

Note this issue does not occurs if there are no simple codes in between the $ and the number example: &c$%test% doesn't cause any issues but $&l%test% does

I've tried using Matcher.quoteReplacement and even tried doing it manually and replacing the $\\$ but nothing worked.

Note: The placeholder %price% needs to be replaced before entering the function

Share Improve this question edited yesterday user85421 29.6k11 gold badges65 silver badges92 bronze badges asked yesterday whydoihavetochoseanamewhydoihavetochoseaname 254 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

Your problem is in the lambda expression you pass to #replaceAll here:

matchResult ->    
   matchResult.group(1) + 
   COLOR_CODES.get(matchResult.group(2));

The result of matchResult.group(1) is sometimes $. You want this interpreted as a literal $ to replace into the string, but $ is special in matcher as documented here. You need to escape any literal $ and \ in the replacement string returned by your lambda.

You can accomplish this using the Matcher.quoteReplacement(...) method documented here.

Here's a fixed version of your code:

 private static final Map<String, String> COLOR_CODES = new HashMap<>() {{
        put("0", "<reset><color:black>");
        put("1", "<reset><color:dark_blue>");
        put("2", "<reset><color:dark_green>");
        put("3", "<reset><color:dark_aqua>");
        put("4", "<reset><color:dark_red>");
        put("5", "<reset><color:dark_purple>");
        put("6", "<reset><color:gold>");
        put("7", "<reset><color:gray>");
        put("8", "<reset><color:dark_gray>");
        put("9", "<reset><color:blue>");
        put("a", "<reset><color:green>");
        put("b", "<reset><color:aqua>");
        put("c", "<reset><color:red>");
        put("d", "<reset><color:light_purple>");
        put("e", "<reset><color:yellow>");
        put("f", "<reset><color:white>");
        put("k", "<obfuscated>");
        put("l", "<bold>");
        put("m", "<strikethrough>");
        put("n", "<underlined>");
        put("o", "<italic>");
        put("r", "<reset>");
    }};

    public static Component translate(String text) {
        // Then apply color codes
        return Colors.interprate(replaceprimitive(text.replaceAll(String.valueOf(LegacyComponentSerializer.SECTION_CHAR), "&")));
    }

    private static String replaceprimitive(String string) {
        return PATTERN.matcher(string).replaceAll(matchResult ->
          Matcher.quoteReplacement(matchResult.group(1) + COLOR_CODES.get(matchResult.group(2))));
    }
}

本文标签: javagetting illegal group reference due to Stack Overflow