-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?!

Hearner
  • 2,711
  • 3
  • 17
  • 34
user3812331
  • 5
  • 2
  • 2

7 Answers7

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");
svdragster
  • 141
  • 2
  • 7
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

Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
  • 3
    `replace` replaces all occurences, not only the first one. See https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence) @Korashen @Navneet Krishna – svdragster Aug 06 '18 at 12:22
  • @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. – Kevin Cruijssen Aug 06 '18 at 12:53
  • 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... – Korashen Aug 06 '18 at 13:08
2

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

String s = "some { string".replaceAll("\\{", "other string");
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
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.

Glains
  • 2,773
  • 3
  • 16
  • 30
1

You need to escape character "{" .

Try this :

"some { string".replaceAll("\\{", "other string");
rav3n6
  • 27
  • 2
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");
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
0
  1. replace: "xxx".replace("{","xxx")
  2. replaceAll: "xxx".replace("\{","xxx")

Difference between String replace() and replaceAll()

Steve Nash
  • 144
  • 6