我正在尝试下面的代码,但出现错误
String x = "aaa XXX bbb";
String replace = "XXX";
String y = "xy$z";
String z=y.replaceAll("$", "\\$");
x = x.replaceFirst(replace, z);
System.out.println(x);
错误
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at java.util.regex.Matcher.replaceFirst(Unknown Source)
at java.lang.String.replaceFirst(Unknown Source)
at Test.main(Test.java:10)
我想要结果为
aaa xy$z bbb