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"));

时间: 2024-12-12 20:44:43