The documentation of String.replaceAll(regex, replacement)
states:
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.
The documentation of Matcher.replaceAll(replacement)
then states:
backslashes are used to escape literal characters in the replacement string
So to put this more clearly, when you replace with \,
, it is as if you were escaping the comma. But what you want is really the \
character, so you should escape it with \\,
. Since that in Java, \
also needs to be escaped, the replacement String becomes \\\\,
.
If you are having a hard time remembering all this, you can use the method Matcher.quoteReplacement(s)
, whose goal is to correctly escape the replacement part. Your code would become:
String replacedValue = neName.replaceAll(",", Matcher.quoteReplacement("\\,"));