Gavin Kistner
6/23/2005 4:36:00 AM
On Jun 22, 2005, at 8:30 PM, tietyt@gmail.com wrote:
> To summarize my whole question: how do I get EVERY match of a regular
> expression (instead of just the first)?
In addition to the correct response given by others (String#scan),
you might also want to look at the StringScanner class. It gives you
the ability to crawl through a string with successive regexp calls,
where each new call starts at the new 'current' position.
story = <<ENDSTORY
Hello World! There are 3 cats in my house, with 4 feet each.
6 of those 12 feet have 5 claws each; the other 6 feet have 4 claws
each.
Ow, my back. 54 claws need clipping.
ENDSTORY
require 'strscan'
scanner = StringScanner.new( story )
info = []
count_nouns = /(\d+) (\w+)/
until scanner.eos?
break unless scanner.scan_until( count_nouns )
tidbit = {
:full_match => scanner[0],
:count => scanner[1].to_i,
:noun => scanner[2]
}
info << tidbit
end
require 'pp'
pp info
info.each{ |tidbit|
puts "Of %7s, I saw %02d" % [ tidbit[:noun], tidbit[:count] ]
}
[{:noun=>"cats", :count=>3, :full_match=>"3 cats"},
{:noun=>"feet", :count=>4, :full_match=>"4 feet"},
{:noun=>"of", :count=>6, :full_match=>"6 of"},
{:noun=>"feet", :count=>12, :full_match=>"12 feet"},
{:noun=>"claws", :count=>5, :full_match=>"5 claws"},
{:noun=>"feet", :count=>6, :full_match=>"6 feet"},
{:noun=>"claws", :count=>4, :full_match=>"4 claws"},
{:noun=>"claws", :count=>54, :full_match=>"54 claws"}]
Of cats, I saw 03
Of feet, I saw 04
Of of, I saw 06
Of feet, I saw 12
Of claws, I saw 05
Of feet, I saw 06
Of claws, I saw 04
Of claws, I saw 54