regex - Replace all instances of a sequence in java -


i want replace instance like

{image:imagename} imagepath ,

{message:messagename} message.

i tried using replaceall function of string class not able achieve 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 {image:abcd}     @ java.util.regex.pattern.error(pattern.java:1713)     @ java.util.regex.pattern.closure(pattern.java:2775)     @ java.util.regex.pattern.sequence(pattern.java:1889)     @ java.util.regex.pattern.expr(pattern.java:1752)     @ java.util.regex.pattern.compile(pattern.java:1460)     @ java.util.regex.pattern.<init>(pattern.java:1133)     @ java.util.regex.pattern.compile(pattern.java:823)     @ java.lang.string.replaceall(string.java:2189)     @ com.example.worksheet.main(worksheet.java:28) 

{ regex metacharacter , should escaped, try this

str = str.replaceall("\\{image:abcd}", "defg"); 

note enough escape {, regex compiler understand } used regular character.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -