just makes me laugh

3 views
Skip to first unread message

Mike Schrag

unread,
Mar 6, 2007, 11:58:16 AM3/6/07
to wot...@googlegroups.com
This one is mostly WO unrelated, but it's crazy and the final
solution is sort of comical, so I just had to post it:

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


Reply all
Reply to author
Forward
0 new messages