使用 replaceAll()
给我一个 rexex 异常。
这是我正在使用的代码:
public class test {
public static void main(String[] args) {
String text= "This is to be replaced &1 ";
text = text.replaceAll("&1", "&");
System.out.println(text);
}
}
异常(exception):
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at java.util.regex.Matcher.replaceAll(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at test.main(test.java:7)
最佳答案
似乎对我来说工作得很好。 http://ideone.com/7qR6Z
但是对于这么简单的事情,您可以避免使用正则表达式,而只需使用 string.replace()
text = text.replace("&1", "&");
关于java - 如何在java中用 "$ "替换任何字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6156381/