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
1 Answer
Reset to default 5Your 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
版权声明:本文标题:java - getting illegal group reference due to $ - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283068a1926845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论