From: Christopher Causer Date: 2008-02-12T18:10:56+09:00 Subject: Re: Return number of matches 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. Just out of interest, why doesn't my first effort work? string.scan(/[^\\]\$/).size ? -- Posted via http://www.ruby-forum.com/.