线程" main"中的例外情况java.util.regex.PatternSyntaxException:索引41附近的未闭合字符类,"元数据" [a-zA-Z0-9:{}" ,+()[] \ _ == *] * [}]

时间:2015-12-16 09:57:41

标签: java

实际元数据对象::

,"Metadata":{"ModifiedDate":"Thu Dec 10 20151046","CreatedDate":"Thu Dec 10 2015 10:46:30 GMT+0530 (IST)","HiddenRule":"[{\"text\":\"\",\"data_type\":\"operator\",\"internal_name\":\"\"},{\"text\":\"Gender\",\"data_type\":\"String\",\"internal_name\":\"gender\",\"class\":\"Open\"},{\"text\":\"\",\"data_type\":\"operator\",\"internal_name\":\"\"},{\"text\":\"==\",\"data_type\":\"operator\",\"internal_name\":\"==\",\"class\":\"Oper\"},{\"text\":\"\",\"data_type\":\"operator\",\"internal_name\":\"\"},{\"text\":\"\\\"sd\\\"\",\"data_type\":\"operator\",\"internal_name\":\"\\\"sd\\\"\",\"class\":\"Cons\"},{\"text\":\"\",\"data_type\":\"operator\",\"internal_name\":\"\"}]"} 

尝试搜索"元数据"对象,在JSON字符串中并用空格替换它。基本上从JSON字符串中删除对象。 ,\"元数据\" [A-ZA-Z0-9:{}" ,+()[] \ _ == ] [}]。但是当在JAVA程序中编译时,我正面临异常,

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 41
,"Metadata"[a-zA-Z0-9:{}" ,+()[]\_==*]*[}]
                                         ^
    at java.util.regex.Pattern.error(Pattern.java:1924)
    at java.util.regex.Pattern.clazz(Pattern.java:2493)
    at java.util.regex.Pattern.sequence(Pattern.java:2030)
    at java.util.regex.Pattern.expr(Pattern.java:1964)
    at java.util.regex.Pattern.compile(Pattern.java:1665)
    at java.util.regex.Pattern.<init>(Pattern.java:1337)
    at java.util.regex.Pattern.compile(Pattern.java:1022)
    at java.lang.String.replaceAll(String.java:2162)
    at temp.main(temp.java:20)

    String str=",\"Metadata\":{\"ModifiedDate\":\"Thu Dec 10 20151046\",\"CreatedDate\":\"Thu Dec 10 2015 10:46:30 GMT+0530 (IST)\",\"HiddenRule\":\"[{\"text\":\"\",\"data_type\":\"operator\",\"internal_name\":\"\"},{\"text\":\"Gender\",\"data_type\":\"String\",\"internal_name\":\"gender\",\"class\":\"Open\"},{\"text\":\"\",\"data_type\":\"operator\",\"internal_name\":\"\"},{\"text\":\"==\",\"data_type\":\"operator\",\"internal_name\":\"==\",\"class\":\"Oper\"},{\"text\":\"\",\"data_type\":\"operator\",\"internal_name\":\"\"},{\"text\":\"\\\"sd\\\"\",\"data_type\":\"operator\",\"internal_name\":\"\\\"sd\\\"\",\"class\":\"Cons\"},{\"text\":\"\",\"data_type\":\"operator\",\"internal_name\":\"\"}]\"}";
        String regexes = ",\"Metadata\"[a-zA-Z0-9:{}\" ,+()[]\\_==*]*[}]";
        System.out.println(str.replaceAll(regexes, "Hi!!!"));

1 个答案:

答案 0 :(得分:0)

][a-zA-Z0-9:{}" ,+()\[\]\_==*\]*[}] 是正则表达式中的特殊字符。你必须逃脱它。

{{1}}