我正在尝试替换\
使用String.replaceAll
,但这样做时出现异常。这是我的代码:
String str = "1^\\";
System.out.println("\nInitial string length: "+str.length()+ " Initial String is: "+str);
String temp = str.replaceAll("[&@;^\\]","");
这是输出:
Initial string length: 3 Initial String is: 1^\
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 6
[&@;^\]
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.clazz(Pattern.java:2254)
at java.util.regex.Pattern.sequence(Pattern.java:1818)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
at java.lang.String.replaceAll(String.java:2190)