From: Patrick Hurley Date: 2007-06-03T13:48:50+09:00 Subject: Re: No way of looking for a regrexp match starting from a particular point in a string? On 6/2/07, 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. > > > Thanks, > Ken > > I don't know of anything obvious, but I would probably do something a little more like: class String def match_each(exp) str = self while md = str.match(exp) yield md str = md.post_match end end end foo = "foo bar foo bar foo" foo.match_each /[oa][or]/ do |md| puts "Found: #{md}" end # pth