Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PatternSyntaxException while trying to split by },{

I am trying to break up an array I got through an API on a site, which Java has retrieved as a String.

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

A PatternSyntaxException is thrown. For some reason, it really doesn't like },{. I have tried escaping it as \{, but it says it is an illegal escape.

What is the proper way to escape this string?

like image 959
CMahaff Avatar asked May 08 '11 06:05

CMahaff


1 Answers

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

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

What is the proper way to escape this String?

Escape the backslashes too, by doubling them. This is for Java string escapes. The escaped backslashes will then escape the braces for the regex.

String[] ex = exampleString.split("\\},\\{");
like image 106
BoltClock Avatar answered Oct 17 '22 10:10

BoltClock