On Sun, Jun 03, 2007 at 12:59:24PM +0900, Kenneth McDonald wrote:
> I'm probably just missing something obvious, but I haven't found a way 
> to match a regular expression against only part of a string, in 
> particular only past a certain point of a string, as a way of finding 
> successive matches. Of course, one could do a match against a string, 
> take the substring past that match and do a match against the substring, 
> and so on, to find all of the matches for the string, but that could be 
> very expensive for very large strings.
> 
> I'm aware of the String.scan method, but that doesn't work for me 
> because it doesn't return MatchData instances.
> 
> What I want is just something like regexp.match(string, n), where the 
> regexp starts looking for a match at or after position n in the string.
require 'strscan'
scanner = StringScanner.new(string)
scanner.pos = n
if scanner.scan(regexp)
  p scanner[1] 
  p scanner.matched
  p scanner.pos
end

It's in the stdlib. (Note, it doesn't actually give you a match data, or
set $~, but of the top of my head I can't think of anything that a
matchdata can do that the stringscanner can't.)

> 
> 
> Thanks,
> Ken