-2

We're using replaceAll method of String and we can't replace { in any string. Our example:

Tried :

"some { string".replaceAll("{", "other string");

And the error is the following:

java.util.regex.PatternSyntaxException: Illegal repetition occurs

Open to any ideas! Maybe there is a workaround?!

3
  • You need to escape it \{ should do the job Commented Aug 6, 2018 at 11:52
  • You might need to escape the character {. Try .replaceAll("\\{", "other string"); Commented Aug 6, 2018 at 11:53
  • Unless you're trying to use regular expressions, you should be using replace, not replaceAll. Commented Aug 6, 2018 at 12:06

7 Answers 7

8

Using replaceAll requires a regular expression (regex)

Try using the replace method instead of replaceAll

"some { string".replace("{", "other string");

or escape the special character in the regex using \\

"some { string".replaceAll("\\{", "other string");
Sign up to request clarification or add additional context in comments.

Comments

5

try with replace() like this

"some { string".replace("{", "other string");

or use replaceAll with following regex format

"some { string".replaceAll("\\{", "your string to replace");

Note : in the case of replace() the first argument is a character sequence, but in the case of replaceAll the first argument is regex

3 Comments

replace replaces all occurences, not only the first one. See docs.oracle.com/javase/6/docs/api/java/lang/… @Korashen @Navneet Krishna
@Korashen Both .replace and .replaceAll replaces all occurrences.. .replaceAll is used with regexes, and .replace without. The name convention just sucks to be completely honest.. .replaceAll and .replaceAllWithRegex or something along those lines would have made more sense. Side note: There is also a .replaceFirst method, which just like .replaceAll only accepts regexes, but will only replace the first occurrence.
Oh boy, you are right. Got something mixed up... deleted my comment as it is wrong. What was it then, that I had in mind...
2

You need to escape the { as it has special meaning in the regex. Use :

String s = "some { string".replaceAll("\\{", "other string");

Comments

1

You need to escape character "{" .

Try this :

"some { string".replaceAll("\\{", "other string");

Comments

1

{ is an indicator to the regex engine that you are about to start a repetition indicator, like {2,4} which means '2 to 4 times of the previous token'.

But {f is illegal, because it has to be followed by a number, so it throws an exception.

You can do something like this

"some { string".replaceAll("\\{", "other string");

Comments

1

You have to use the escape character \\:

"some { string".replaceAll("\\{", "other string");

The character { is reserved in regular expressions, thats why you have to escape it to match the literal. Alternatively, you can use replace to only consider a CharSequence, not a regular expression.

Comments

0
  1. replace: "xxx".replace("{","xxx")
  2. replaceAll: "xxx".replace("\{","xxx")

Difference between String replace() and replaceAll()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.