From: Gavin Kistner Date: 2005-06-23T13:36:06+09:00 Subject: Re: Getting a list of results from one regular expression 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 = < 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