2015-12-16 54 views
-3

實際的元數據對象::線程「main」中的異常java.util.regex.PatternSyntaxException:在索引41「元數據」附近未封閉的字符類[a-zA-Z0-9:{}「,+()[] _ == *] * [}]

,"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字符串中刪除的對象。 ,\ 「Metadata」[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!!!")); 
+2

這就是你得到的與正則表達式解析JSON。 – Mena

回答

0

[]在正則表達式特殊字符。你必須逃避它。

[a-zA-Z0-9:{}" ,+()\[\]\_==*\]*[}] 
+0

在新代碼中轉義了它。但Matcher類的find方法對於正則表達式的最後部分返回false,即[] ] – user3083516

相關問題