From: William James Date: 2008-02-13T00:14:55+09:00 Subject: Re: Return number of matches On Feb 12, 3:10 am, Christopher Causer wrote: > William James wrote: > > On Feb 11, 8:41 am, Jano Svitok 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. You said that you wanted to count all dollar signs except the escaped ones. So the correct answer is 2. The first backslash escapes the second one, which escapes nothing.