Regex.Replace, $ and $$

Just hit an interesting problem with the replacement argument of Regex.Replace. The issue was that I was passing in variable content to this argument, and it turned out that dollar signs could occur in the content I was passing in. These were interpreted as backreferences, which gave me very different results to the ones I was expecting.

With hindsight, this is kind of obvious, and the solution is simply to replace any single dollar signs in the variable content with repeated dollar signs, as described here:
MSDN on Replacement Substitutions (new window)

See if you can spot any whiff of this looking at the documentation for the method though:
MSDN on Regex.Replace(String, String, String, RegexOptions) (new window)

Leave a Reply

Your email address will not be published. Required fields are marked *