I need to escape the $
and therefore I need to replace all occurrences of $
with \$
So I wrote this method:
// String#replaceAll(String regex, String replacement)
public String escape$(String str) {
// the first \\$ to escape it in regular expression
// the second is a normal String so \\$ should mean \$
return str.replaceAll("\\$", "\\$");
}
String s = "$some$$text here";
System.out.println(escape$(s));
Before I submitted for production use, I thought hmmm let's test that even though I was certain it should work. And so I did...
Well you guessed it. It doesn't work! It returns the same thing!
// expected result of the above: \$some\$\$text here
// reality: $some$$text here
So why doesn't this work?!