Replace all instances of a sequence in java

I want to replace all instance like

{IMAGE:imagename} with imagepath ,

{MESSAGE:messagename} with message.

I tried using replaceAll function of string class but not able to achieve the same.

Example code :

String str = "HELLO WORLD {IMAGE:abcd}";
str = str.replaceAll("{IMAGE:abcd}", "defg");

Output :

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
    at java.util.regex.Pattern.error(
    at java.util.regex.Pattern.closure(
    at java.util.regex.Pattern.sequence(
    at java.util.regex.Pattern.expr(
    at java.util.regex.Pattern.compile(
    at java.util.regex.Pattern.<init>(
    at java.util.regex.Pattern.compile(
    at java.lang.String.replaceAll(
    at com.example.WorkSheet.main(


{ is a regex metacharacter and should be escaped, try this

str = str.replaceAll("\\{IMAGE:abcd}", "defg");

note that it is enough to escape {, regex compiler will understand that } is used as a regular character.

Need Your Help

Why don't I have to release managedObjectContext in the 2nd TableViewController

iphone cocoa-touch core-data memory-leaks release-management

I have two tableview controllers showing CoreData objects. One is a detail view (with sentences) one is an overview (with stories).

Yii url with server static IP address

php url yii

I'm developping with Yii, and I need to send mail to contacts with an link. The proble is that the code