nobu.nokada / softhome.net writes: > At Mon, 7 Jan 2002 10:30:17 +0900, > Matt Armstrong <matt+dated+1010799012.80728c / lickey.com> wrote: >> Since this is a fairly common thing to do (witness the number of users >> of strscan) could we change Regexp#match to take an optional starting >> offset? Then the code could be: > > I agree, and once posted a patch, [ruby-dev:12781]. > > Or you can use String#index: > > offset = 0 > until offset == string.length > if offset = string.index(regexp1, offset) > # do something > elsif offset = string.index(regexp2, offset) > # do something > end > offset += $&.length # forward > end > > Probably, matched strigs are much less than the source string. I liked your idea at first, but this method is actually slower in my case. Why? String#index with an offset does not support \A or ^ in the regexp. Is this a bug in String#index? Shouldn't this be the case? "abc".index(/\Ab/, 1) ==> 1 Without the \A anchor, using String#index for "lex" style parsing is not useful, since you must use regexps without \A, and so each time you test for a regexp it may search to the end of the string. This turns an O(n) algorithm into O(n^2). -- matt