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 \" \' \ )"
This is my code, and where the error occurs:
String[] strArr = jsonText.split("\{([^}]*.?)\}");
What am I doing wrong?
To match literal curly braces, you have to escape them with \ . However, Apex Code uses \ as an escape, too, so you have to "escape the escape". You'll need to do this almost every time you want to use any sort of special characters in your regexp literally, which will happen more frequently than not.
The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.
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:
String[] strArr = jsonText.split("\\{([^}]*.?)\\}");
1. Curle braces have no special meaning here for regexp language, so they should not be escaped I think.
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.
There are good JSON parsing libraries https://stackoverflow.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"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With