1

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 ?

1
  • 1
    { is not a valid regexp. Commented Jun 4, 2013 at 9:58

3 Answers 3

3

You need to escape {:

String regex = getData.replaceAll("\\{", "").replaceAll("\\}", "");
Sign up to request clarification or add additional context in comments.

1 Comment

works, but it is not needed to use a regex for just matching a plain char
0

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("\\{|\\}", "");

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.