Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.regex.PatternSyntaxException: Unmatched closing ')' : during string.split operation

Tags:

java

regex

split

I am trying to perform a split similar the following:

String str = "({Somestring 1 with a lot of braces and commas}),({Somestring 12with a lot of braces and commas})";
println str.split("}),({");

But i see:

java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 0 }),({

Clearly , my string is being treated as a regular expression.

Is there a way i can escape this string?

like image 670
Abhidemon Avatar asked Jun 15 '15 07:06

Abhidemon


1 Answers

the character ( and ) and { and } are special character in regexp. you have to escape these:

println str.split("\\}\\),\\(\\{");
like image 151
Jens Avatar answered Nov 15 '22 06:11

Jens