I want to parse nested JSON strings by splitting them up recursively by { }. The regex I came up with is "{([^}]*.?)}", which I've tested appropriately grabs the string I want. However, when I try to include it in my Java I get the following error: "Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )"
我想通过用{}递归地分割嵌套的JSON字符串来解析它们。我想出的正则表达式是“{([^ }]*。?)}”,我已经测试了适当地抓住我想要的字符串。然而,当我尝试在Java中包含它时,我得到了以下错误:“无效转义序列(有效的转义序列是\b \t \n \f \f \r \r \“\”\)”
This is my code, and where the error occurs:
这是我的代码,错误发生的地方:
String[] strArr = jsonText.split("\{([^}]*.?)\}");
What am I doing wrong?
我做错了什么?
5 个解决方案
#1
11
1. Curle braces have no special meaning here for regexp language, so they should not be escaped I think.
1。Curle花括号在这里对于regexp语言没有特殊的意义,所以我认为它们不应该被忽略。
-
If you want to escape them, you can. Backslash is an escape symbol for regexp, but it also should be escaped for Java itself with second backslash.
如果你想逃离他们,你可以。反斜杠是regexp的转义符号,但是对于Java本身,也应该用第二个反斜杠转义。
-
Why don't you escape curle brace inside grouping construct?
为什么你不能在分组结构中避免弯曲括号呢?
-
There are good JSON parsing libraries https://*.com/questions/338586/a-better-java-json-library
有很好的JSON解析库https://*.com/questions/338586/a-better-java-json-library
-
You are using reluctant quantifier, so it won't work with nested braces, for example for
{"a", {"b", "c"}, "d"}
it will match{"a", {"b", "c"}
您正在使用勉强量词,因此它不能使用嵌套的大括号,例如{a" a", {"b", "c"}, "d"}它将匹配{a", "b", "c"}
#2
8
The nasty thing about Java regexes is that java doesn't recognize a regex as a regex.
It accepts only \\
, \'
, \"
or \u[hexadecimal number]
as valid escape sequences. You'll thus have to escape the backslashes because obviously \{
is an invalid escape sequence.
Corrected version:
Java regexes令人讨厌的地方是Java不承认regex是regex。它只接受\、\、\或\u[十六进制数字]作为有效的转义序列。因此,您必须转义反斜杠,因为显然\{是无效的转义序列。修正版:
String[] strArr = jsonText.split("\\{([^}]*.?)\\}");
#3
2
Double the backslashes:
双反斜杠:
String[] strArr = jsonText.split("\\{([^}]*.?)\\}");
#4
2
The regular expression should be
正则表达式应该是
"\\{([^}]*?)\\}"
.
is not required!
。不需要!
#5
1
You need to escape your backslash with one more backslash. Since, \{
is not a valid escape sequence: -
你需要再用一个反斜杠来转义你的反斜杠。因为,\{不是一个有效的转义序列:-
String[] strArr = jsonText.split("\\{([^\\}]*.?)\\}");
You can refer to Pattern documentation for more information about escape sequences.
您可以参考模式文档了解更多关于转义序列的信息。
#1
11
1. Curle braces have no special meaning here for regexp language, so they should not be escaped I think.
1。Curle花括号在这里对于regexp语言没有特殊的意义,所以我认为它们不应该被忽略。
-
If you want to escape them, you can. Backslash is an escape symbol for regexp, but it also should be escaped for Java itself with second backslash.
如果你想逃离他们,你可以。反斜杠是regexp的转义符号,但是对于Java本身,也应该用第二个反斜杠转义。
-
Why don't you escape curle brace inside grouping construct?
为什么你不能在分组结构中避免弯曲括号呢?
-
There are good JSON parsing libraries https://*.com/questions/338586/a-better-java-json-library
有很好的JSON解析库https://*.com/questions/338586/a-better-java-json-library
-
You are using reluctant quantifier, so it won't work with nested braces, for example for
{"a", {"b", "c"}, "d"}
it will match{"a", {"b", "c"}
您正在使用勉强量词,因此它不能使用嵌套的大括号,例如{a" a", {"b", "c"}, "d"}它将匹配{a", "b", "c"}
#2
8
The nasty thing about Java regexes is that java doesn't recognize a regex as a regex.
It accepts only \\
, \'
, \"
or \u[hexadecimal number]
as valid escape sequences. You'll thus have to escape the backslashes because obviously \{
is an invalid escape sequence.
Corrected version:
Java regexes令人讨厌的地方是Java不承认regex是regex。它只接受\、\、\或\u[十六进制数字]作为有效的转义序列。因此,您必须转义反斜杠,因为显然\{是无效的转义序列。修正版:
String[] strArr = jsonText.split("\\{([^}]*.?)\\}");
#3
2
Double the backslashes:
双反斜杠:
String[] strArr = jsonText.split("\\{([^}]*.?)\\}");
#4
2
The regular expression should be
正则表达式应该是
"\\{([^}]*?)\\}"
.
is not required!
。不需要!
#5
1
You need to escape your backslash with one more backslash. Since, \{
is not a valid escape sequence: -
你需要再用一个反斜杠来转义你的反斜杠。因为,\{不是一个有效的转义序列:-
String[] strArr = jsonText.split("\\{([^\\}]*.?)\\}");
You can refer to Pattern documentation for more information about escape sequences.
您可以参考模式文档了解更多关于转义序列的信息。