# How to capture all conditions using regex

**URL:** <https://www.ruby-forum.com/t/how-to-capture-all-conditions-using-regex/181036>\
**Category:** Ruby\
**Created:** [January 4, 2010, 8:53am UTC](https://www.ruby-forum.com/t/how-to-capture-all-conditions-using-regex/181036 "2010-01-04T08:53:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 8:53am UTC](https://www.ruby-forum.com/t/how-to-capture-all-conditions-using-regex/181036/1 "2010-01-04T08:53:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 9:17am UTC](https://www.ruby-forum.com/t/how-to-capture-all-conditions-using-regex/181036/2 "2010-01-04T09:17:22Z")

</div>

The regex itself seems okay…  
irb(main):015:0\> x.scan(/\d{3}/)  
=\> [“123”, “456”, “789”]

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 9:17am UTC](https://www.ruby-forum.com/t/how-to-capture-all-conditions-using-regex/181036/3 "2010-01-04T09:17:57Z")

</div>

> “789”, not the only “123”.  
> How to do it (must use regex)? Thanks.

Try to use the block version of gsub:

> > x.gsub(/\d{3}/) {|m| puts m}  
> > 123  
> > 456  
> > 789

Cheers,

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 9:19am UTC](https://www.ruby-forum.com/t/how-to-capture-all-conditions-using-regex/181036/4 "2010-01-04T09:19:08Z")

</div>

On Mon, Jan 4, 2010 at 8:53 AM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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](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.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 2:05pm UTC](https://www.ruby-forum.com/t/how-to-capture-all-conditions-using-regex/181036/5 "2010-01-04T14:05:21Z")

</div>

On 01/04/2010 09:33 AM, Ruby N. wrote:

> Thanks all for the quick response.  
> In perl there is a “/g” option which can be used for multi-matching.
> 
> # perl -le ‘$x=“123 456 789”; @x=$x=~/\d{3}/g; print “@x”’
> 
> 123 456 789
> 
> I try to find ruby’s instead one, but never thought there is already a  
> scan method available.

Note that even with #scan you have two options: you can either take the  
Array returned or use the block form to do something whenever a match  
occurs:

> irb(main):004:0\> s = “123 456 789”  
> =\> “123 456 789”  
> irb(main):005:0\> s.scan /\d+/  
> =\> [“123”, “456”, “789”]  
> irb(main):006:0\> s.scan /\d+/ do |match| p match end  
> “123”  
> “456”  
> “789”  
> =\> “123 456 789”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 4, 2010, 9:33am UTC](https://www.ruby-forum.com/t/how-to-capture-all-conditions-using-regex/181036/6 "2010-01-04T09:33:46Z")

</div>

Thanks all for the quick response.  
In perl there is a “/g” option which can be used for multi-matching.

# perl -le ‘$x=“123 456 789”; @x=$x=~/\d{3}/g; print “@x”’

123 456 789

I try to find ruby’s instead one, but never thought there is already a  
scan method available.  
Thanks.
