PatternSyntaxException when trying to split by}, {

I am trying to split the array that I received through the API on the site that Java received as String .

 String[] ex = exampleString.split("},{"); 

A PatternSyntaxException . For some reason, I really don't like it },{ . I tried to run away from him as \{ but he says this is an illegal exit.

What is the correct way to avoid this line?

+7
source share
1 answer

For some reason, I really don't like it}, {.

This is because curly braces ( } and { ) are special characters in Java regular expressions. If you try to use them literally without escaping, it will consider a syntax error, hence your exception.

What is the correct way to avoid this line?

Also avoid backslashes by doubling them. This is for the Java escape sequence. Torn backslashes will then escape the brackets for regex.

 String[] ex = exampleString.split("\\},\\{"); 
+15
source

All Articles