Can't replace string with regex in java -


this question has answer here:

i'm trying replace string occurrence in query this:

select * db age = 3 , name = 'value' 

i'm using regex: name\s*=\s*'[a-z]+' works fine here

this how i've tried in java:

query.replaceall("name\s*=\s*'[a-z]+'", replacementstring); // gives me invalid escape sequence message query.replaceall("name\\s*=\\s*'[a-z]+'", replacementstring);   pattern pattern = pattern.compile("name*= *'[a-z]*'"); query.replaceall(pattern.pattern(), replacementstring);  pattern pattern2 = pattern.compile("name\\s*=\\s*'[a-z]*'");                 query.replaceall(pattern2.pattern(), replacementstring); 

none of work.

how can replace name = 'value' occurrence?

java strings immutable.

you need assign result of replaceall call string:

query = query.replaceall(...); 

Comments