Let's take a tour of String#repalceAll(String regex, String replacement)
You will see that:
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
So lets take a look at Matcher.html#replaceAll(java.lang.String) documentation
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. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
You can see that in replacement
we have special character $
which can be used as reference to captured group like
System.out.println("aHellob,aWorldb".replaceAll("a(\\w+?)b", "$1"));
// result Hello,World
But sometimes we don't want $
to be such special because we want to use it as simple dollar character, so we need a way to escape it.
And here comes \
, because since it is used to escape metacharacters in regex, Strings and probably in other places it is good convention to use it here to escape $
.
So now \
is also metacharacter in replacing part, so if you want to make it simple \
literal in replacement you need to escape it somehow. And guess what? You escape it the same way as you escape it in regex or String. You just need to place another \
before one you escaping.
So if you want to create \
in replacement part you need to add another \
before it. But remember that to write \
literal in String you need to write it as "\\"
so to create two \\
in replacement you need to write it as "\\\\"
.
So try
s = s.replaceAll("'", "\\\\'");
Or even better
to reduce explicit escaping in replacement part (and also in regex part - forgot to mentioned that earlier) just use replace
instead replaceAll
which adds regex escaping for us
s = s.replace("'", "\\'");
"You are 'awesome'\'amazing'"
, then you currently would get"You are \'awesome\'\\'amazing\'"
. That leaves the 3rd quote unescaped because that user-entered backslash is escaping the generated backslack after it! – Triangulation