Java can't replace “{” with replaceAll method

  • A+
Category:Languages

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

 


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"); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: