Your problem here is that '$' is a regular expression metacharacter. That is, it is of particular importance. In particular, "$" means "end of line".
Since you do not have metacharacters in your template, you can use String.replace() instead, which simply replaces literal strings.
s = s.replace("$","\\$");
However, if you really want to use replaceAll() (for example, if other parts of your template should be metacharacters):
If you want to match the actual "$", you need to escape the "$" in the template to make it literal "$".
\$
Then you also need to avoid quoting '\' for Java, so you get:
s = s.replaceAll("\\$","\\$");
However, '$' is still the metacharacter in the second parameter, so we need more:
s = s.replaceAll("\\$",Matcher.quoteReplacement("\\$"));
Together with your other replacement:
s = s.replaceAll("\\$",Matcher.quoteReplacement("\\$")).replaceAll("\n","");
source share