Java regex to match curly braces - "invalid escape sequence"

1. Curle braces have no special meaning here for regexp language, so they should not be escaped I think.

  1. 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.

  2. There are good JSON parsing libraries https://stackoverflow.com/questions/338586/a-better-java-json-library

  3. 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"}


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("\\{([^}]*.?)\\}");

Tags:

Java

Regex