I am matching a pattern which is a string "test" in sentence "you are test" and replace it with "test\".
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile("test", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("you are test");
while (matcher.find()) {
matcher.appendReplacement(sb, "test\"));
}
matcher.appendTail(sb);
replacementString = sb.toString();
The issue is in the statement matcher.appendReplacement(sb, "test\")); where "\" is considered as special character and replacement is not performed.
Please share your thoughts on how overcome this scenario and how to replace with a "\" in the string.