From: Michael Granger Date: 2002-12-19T00:36:38+09:00 Subject: Re: [RCR] Global Regexp Match Mechanism (//g) Austin Ziegler (in "[RCR] Global Regexp Match Mechanism (//g)") wrote: > I played around with the necessary mechanisms to emulate //g Regexp > behaviour in Ruby, but I'm not too happy about how much extra work I > would have to do. > > It would be very nice/useful to be able to do: > > while md = /foo/g.match(foostr) > puts md.to_s > foostr = md.post_match > end > > IMO, this would involve modifications to both Regexp and String > classes. Strings would need to carry around the position of the last > match against the string. Regexp would have a new method, #gmatch > (instead of //g). > > Alternatively, instead of modifying String, maybe MatchData could be > modified and the new Regexp#gmatch could accept a MatchData or a > String. Perhaps a separate library is not what you want, but 1.7.x at least includes the 'strscan' library by Minero Aoki, which does exactly what you want without making lots of intermediate strings. It's also much faster than Ruby's native String#scan, and maintains position in a thread-safe way by copying the scanned string into a scanner object. I'm using it in my port of Perl's Parse::RecDescent, and it has worked flawlessly. -- Michael Granger Rubymage, Believer, Architect The FaerieMUD Consortium