1

I have an issue with replaceAll function of Java string

replaceAll("regex", "replacement");

works fine but whenever my "replacement" string contains the substring like "$0", "$1" .e.t.c, it will create problem by substituting these $x's with corresponding matching group.

For instance

input ="NAME";
input.replaceAll("NAME", "HAR$0I");

will result in a string "HARNAMEI" as the replacement string contains "$0" which will be substituted by matching group "NAME". How can I override that nature. I need to get the result string as "HAR$0I" only.

I escaped the $ .i.e I converted the replacement string to "HAR\\$0I" which worked fine. But I am looking for any method in java that will do this for me for all such characters which has special meaning in regex world.

2
  • It sounds like you shouldn't actually be using regex / replaceAll(). Commented Apr 17, 2013 at 18:46
  • replaceAll assumes the first parameter as regex and that is the reason the matching group "$0" contains the first parameter for back reference purpose in this case "NAME" Commented Apr 17, 2013 at 18:50

3 Answers 3

1

The documentation of java.lang.String.replaceAll() says:

Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

The documentation of String quoteReplacement(String s) says:

Returns a literal replacement String for the specified String. This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! Please don't forget to approve this answer ;-)
0

$ in replacement is special character allowing you to use groups. To make it literal you will need to escape it with \$ which needs to be written as "\\$". Same rule apply for \, since it is special character used to escape $. If you would like to use \ literal in replacement you would also need to escape it with another \, so you would need to write it as \\\\.

To simplify this process you can just use Matcher.quoteReplacement("yourReplacement")).


In case where you don't need to use regular expression you can simplify it even more and use

replace("NAME", "HAR$0I") 

instead of

replaceAll("NAME", Matcher.quoteReplacement("HAR$0I")) 

Comments

0

It sounds like you're actually trying to replace raw strings, without using regexes at all.

You should simply call String.replace(), which does literal replacements without using regexes.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.