From: Robert Klemme Date: 2007-11-19T06:45:03+09:00 Subject: Re: Count substrings from a string On 18.11.2007 19:57, Robert Dober wrote: > On Nov 18, 2007 6:29 PM, Wolfgang Nádasi-Donner wrote: >> Robert Dober wrote: >>> ...wonder how this would scale if there were a Regexp#match allowing for an offset. >> >> ...which will come in Ruby 1.9: >> >> C:\Dokumente und Einstellungen\wolfgang>irb19 >> irb(main):001:0> "abcdef".match(/./,3) >> => # >> > Great :) > > I have written a 1.9 test and still lookahead is much better, I am > surpprised as I got rid of all fancy stuff in the > bf_sub method going back to my Regexp stuff happily again. IMHO it is not surprising because the lookahead method uses one method (scan) which is implemented in C while you create a lot of temporary objects and also start scanning at *every* position in the string. Usually regexp engines are implemented much smarter than we can do in short method. I am sure you have heard of Boyer Moore and Knuth Morris Pratt algorithms... :-) > def bf_sub substr > r = [] > size.times do |i| > s = > substr.match(self,i) > r << ( s && s[0] ) > end > r.compact > end Kind regards robert