Take this string definition:
String value = "\\$#,###.00";
... and I want to replace all occurrences of an escaped dollar sign
with just a dollar sign (this is actually from a bug with a inline
bindings, where you need to be able to escape a leading dollar sign
that really should be a string literal dollar sign). You'd think
this would be a simple regex replacement to write. Have fun with
escaping :) It turns out, the final proper replacement is:
value = value.replaceAll("\\\\\\$", "\\$");
... that's 6, count them, SIX, backslashes. The explanation for this:
param one:
$ = match end of line
\$ = escape that in the regex
\\$ = escape \ in a java string
\\\\$ = we need match a backslash (double backslash for java) in
front of the escaped $
\\\\\\$ = but we need to escape THAT one, or it would be
interpreted as a regex escape \ (double backslash for java)
param two:
little easier here .. in the second param, $ is a variable
reference, so just java escape it "once" (.. er .. twice .. for java)
\\$
OK .. WO content to return tomorrow :)
ms