Dimitri
Dimitri

Reputation: 687

PatternSyntaxException: String.replaceAll() android

I want to remove all { } as follow:

String regex = getData.replaceAll("{", "").replaceAll("}", "");

but force close my app with log.

java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX

what have i done wrong ?

Upvotes: 1

Views: 1881

Answers (3)

donfuxx
donfuxx

Reputation: 11321

For what you want to do you don't need to use a regex!

You can make use of the replace method instead to match specific chars, which increases readability a bit:

String regex = getData.replace("{", "").replace("}", "");

Escaping the \\{ just to be able to use replaceAll works, but doesn't make sense in your case

Upvotes: 0

Flexo
Flexo

Reputation: 2556

Curly brackets are used to specify repetition in regex's, therefore you will have to escape them.

Furthermore, you should also consider removing all the brackets in one go, instead of called replaceAll(String, String) twice.

String regex = getData.replaceAll("\\{|\\}", "");

Upvotes: 0

Jean Logeart
Jean Logeart

Reputation: 53839

You need to escape {:

String regex = getData.replaceAll("\\{", "").replaceAll("\\}", "");

Upvotes: 3

Related Questions