Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

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?

like image 269
user1436111 Avatar asked Oct 11 '12 16:10

user1436111


People also ask

Do curly braces need to be escaped in regex?

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.

What does \\ mean in Java regex?

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.


Video Answer


2 Answers

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("\\{([^}]*.?)\\}");
like image 68
11684 Avatar answered Oct 19 '22 03:10

11684


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

like image 26
Suzan Cioc Avatar answered Oct 19 '22 04:10

Suzan Cioc