[RCR] Global Regexp Match Mechanism (//g)

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.

-a

···


austin ziegler
Sent from my Treo

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

foostr.scan(/foo/) do |match|
puts match[0]
end

···

On Fri, 13 Dec 2002, Austin Ziegler wrote:


austin ziegler
Sent from my Treo


Greg Millam
walker at deafcode.com

Hi –

···

On Fri, 13 Dec 2002, Austin Ziegler 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.

What do you consider “extra”? :slight_smile: Usually I just use String#scan
for that.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

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 ged@FaerieMUD.org
Rubymage, Believer, Architect
The FaerieMUD Consortium http://www.FaerieMUD.org/

dblack@candle.superlink.net writes:

Hi –

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.

What do you consider “extra”? :slight_smile: Usually I just use String#scan
for that.

In addition to Austin’s reasons, String#scan makes lots of little
intermediate strings, which you don’t necessarily need. So there is
an efficiency argument too.

···

On Fri, 13 Dec 2002, Austin Ziegler wrote: