I was using String.replaceAll(String, String)
when I noticed that replacing a string with $
symbols around it would not work. Example $REPLACEME$
would not be replaced in a Linux system. Anyone know why this is?
Some code:
String foo = "Some string with $REPLACEME$";
foo = foo.replaceAll("$REPLACEME$", "characters");
System.out.println(foo);
Output:
Some string with $REPLACEME$
$
is a special character that needs to be escaped:
foo = foo.replaceAll("\\$REPLACEME\\$", "characters");
Or more generally use Pattern.quote
which will escape all metacharacters (special characters like $
and ?
) into string literals:
foo = foo.replaceAll(Pattern.quote("$REPLACEME$"), "characters");