0

I need to replace <keyword> with a string.

String result = "<keyword>".replace(Pattern.quote("<keyword>"), "text");

Expected result: text

Got result: <keyword>

Any ideas?

1
  • String result = "<keyword>".replace("<keyword>", "text"); Commented Oct 6, 2016 at 18:46

2 Answers 2

3

As String.replace doesn't use regex parameters you can just use

String result = "<keyword>".replace("<keyword>", "text");
Sign up to request clarification or add additional context in comments.

2 Comments

for some reason i thought replace expected regex
replaceAll expects regex , replace expects normal string
0

It seems that you want to use replaceAll which uses a regex instead of replace that doesn't.

String result = "<keyword>".replaceAll("<keyword>", "text");

If you check the replaceAll javadocs you will find:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.