On Feb 12, 2008 10:10 AM, Christopher Causer <chy.causer / gmail.com> wrote: > William James wrote: > > On Feb 11, 8:41 am, Jano Svitok <jan.svi... / gmail.com> wrote: > >> Easy solution: count $ and subtract \$ ;-) > >> > >> string.scan(/\$/).size - string.scan(/\\\$/).size > >> > >> It's possible to do it in one regex, but it won't be nice and elegant > >> and I don't have the time now to try it... > > > > That fails when the backslash preceding a $ > > is itself escaped. > > > >>> irb --prompt xmp > > string = "$\\\\$" > > ==>"$\\\\$" > > string.scan(/\$/).size - string.scan(/\\\$/).size > > ==>1 > > Erm, not sure what you're trying to say. One is the answer I would have > wanted. Thanks anyway for the one liner though. William's tring to say that you have three interesting items in your string, and you haven't counted one: a plain dollar $, escaped dollar \$ and escaped backslash \\. If escaped backslash precedes unescaped dollar, \\$ in your definition of the problem (and in my solution) it is not counted.