Question: I'm trying to escape a RegExp metacharacter in Java. Below is what I want:

```
INPUT STRING: "This is $ test"
OUTPUT STRING: "This is \$ test"

```

This is what I'm currently doing but it's not working:

```
String inputStr= "This is $ test";
inputStr = inputStr.replaceAll("$","\\$");

```

But I'm getting wrong output:

```
"This is $ test$"

```

Answer: 
You have to put 6 backslashes so you escape the backslash and escape the metachar:

`inputStr.replaceAll("\\$","\\\\\\$");`
The first argument to replaceAll is infact a regexp, and the $ actually means "match the end of the string". You can just use replace instead, which doesn't use regexp, just a normal string replace, to achieve what you want in this case. If you want to use a regexp, just escape the $ in the first argument.