首页 > 代码库 > Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^

出现此错误应该是字符转义出现问题:

System.out.println(str.replaceAll("[", "22"));
解决方案:在[之前加上\\
System.out.println(str.replaceAll("\\[", "22"));

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^