Matching in a comma-separated list with Regular Expressions

Tonight’s quiz:
How to see if ‘foo’ is a unique entry in comma-separated list ‘foo,bar,foobar,foo,foo’?

We want to match the ‘foo’ at the beginning, at the end, and also in the middle, but not as part of the word ‘foobar.’ Also the comma-separated list might only be ‘foo’.

First attempt:
[^,]foo[$,]

Plain wrong, because ^ means something different inside a set

Second attempt:
^foo$|^foo,|,foo,|,foo$

Works, but clunky.

Third attempt:
bfoob

Works perfectly. Especially when you remember to escape the ” in your .NET string assignment ­čÖü

Thanks to the Rad Software Regular Expression Designer too!

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)