Q:

Matcher's appendReplacement method ignores the replacement's backslashes

I have a string s and a regex. I would like to replace each match of the regex in s with a replacement string. The replacement string might contain one or more backslashes. To perform the replacement, I'm using Matcher's appendReplacement method.
The problem with appendReplacement is that it ignores all the backlashes it encounters in the replacement string. So if I try replacing the substring "match" in the string "one match" with the replacement string "a\\b", then appendReplacement results in "one ab" instead of "one a\\b"*:
Matcher matcher = Pattern.compile("match").matcher("one match");
StringBuffer sb = new StringBuffer();
matcher.find();
matcher.appendReplacement(sb, "a\\b");
System.out.println(sb); // one ab

I have taken a look at appendReplacement's code, and found out that it skips any encountered backslash:
if (nextChar == '\\') {
    cursor++
    nextChar = replacement.charAt(cursor);
    ...
}

How can I replace each match with a replacement string that contains backslashes?
(*) - Note that there's a single backslash in "a\\b", not two. The backslash is just escaped.

A:

You need to double escape the backslash i.e.:
matcher.appendReplacement(sb, "a\\\\b");

Full Code:
Matcher matcher = Pattern.compile("match").matcher("one match");
sb = new StringBuffer();
matcher.find();
matcher.appendReplacement(sb, "a\\\\b");
System.out.println(sb); //-> one a/b

Reason being that Java allows you to use backreferences like $1, $2 etc in replacement string and that enforces same escaping mechanism for backslash as in the main regex.

