From: "Jesús Gabriel y Galán" Date: 2010-01-04T17:16:53+09:00 Subject: Re: how to capture all conditions using regex On Mon, Jan 4, 2010 at 8:53 AM, Ruby Newbee wrote: > Hello, > > irb(main):001:0> x="123 456 789" > => "123 456 789" > > irb(main):003:0> if /\d{3}/ =~ x > irb(main):004:1>   puts $& > irb(main):005:1> end > 123 > > > For the statement above, I want to capture all "123" and "456" and > "789", not the only "123". > How to do it (must use regex)? Thanks. Use the scan method: http://ruby-doc.org/core/classes/Kernel.html#M005985 irb(main):001:0> x="123 456 789" => "123 456 789" irb(main):002:0> x.scan /\d{3}/ => ["123", "456", "789"] Jesus.