# Regexp multiple matches

**URL:** <https://www.ruby-forum.com/t/regexp-multiple-matches/158817>\
**Category:** Ruby\
**Created:** [January 28, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/regexp-multiple-matches/158817 "2009-01-28T12:29:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [January 28, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/regexp-multiple-matches/158817/1 "2009-01-28T12:29:17Z")

</div>

Hi,

/(\d+),(\d+)(;(\d+),(\d+))\*/

This regexp matches are:  
0: (5,1;5,7;3,2)  
1: (5)  
2: (1)  
3: (;3,2)  
4: (3)  
5: (2)

My question is why ;5,7 is not matched ?

Thanks,  
Mickael.

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [January 28, 2009, 1:08pm UTC](https://www.ruby-forum.com/t/regexp-multiple-matches/158817/2 "2009-01-28T13:08:10Z")

</div>

Hi –

On Wed, 28 Jan 2009, Mickael Faivre-Macon wrote:

> 5: (2)
> 
> My question is why ;5,7 is not matched ?

What’s the string?

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [January 28, 2009, 1:29pm UTC](https://www.ruby-forum.com/t/regexp-multiple-matches/158817/3 "2009-01-28T13:29:58Z")

</div>

On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> 5: (2)
> 
> My question is why ;5,7 is not matched ?
> 
> ## Thanks, Mickael.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Mickael

Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2  
which  
matches based on the ‘\*’

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [January 28, 2009, 4:03pm UTC](https://www.ruby-forum.com/t/regexp-multiple-matches/158817/4 "2009-01-28T16:03:10Z")

</div>

And is there a way to keep previous matching ?  
Mickael.

Andrew T. wrote:

> On Wed, Jan 28, 2009 at 1:26 PM, Mickael Faivre-Macon  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > 5: (2)
> > 
> > My question is why ;5,7 is not matched ?
> > 
> > ## Thanks, Mickael.
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Mickael
> 
> Because it matches ;5,7 first and then overwrites $3,$4 & $5 with ;3,2  
> which  
> matches based on the ‘\*’
> 
> –  
> Andrew T.  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> “I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [January 28, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/regexp-multiple-matches/158817/5 "2009-01-28T17:20:53Z")

</div>

On Wed, Jan 28, 2009 at 5:00 PM, Mickael Faivre-Macon  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > My question is why ;5,7 is not matched ?  
> > > which  
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

What about tackling it from a completely different angle

s = “5,1;5,7;3,2”  
pairs = s.split(‘;’) #=\> [“5,1”, “5,7”, “3,2”]  
pairs.each do |pair|  
pair.split(‘,’) #=\> [“5”, “1”] etc  
end

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [January 28, 2009, 5:57pm UTC](https://www.ruby-forum.com/t/regexp-multiple-matches/158817/6 "2009-01-28T17:57:25Z")

</div>

On Jan 28, 2009, at 10:00 AM, Mickael Faivre-Macon wrote:

> > > –
> > 
> > Andrew T.  
> > [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> > [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> > 
> > “I have never let my schooling interfere with my education” - Mark  
> > Twain
> 
> And is there a way to keep previous matching ?  
> Mickael.

> –

irb\> re = /(\d+),(\d+)(;(\d+),(\d+))_/  
=\> /(\d+),(\d+)(;(\d+),(\d+))_/  
irb\> s = “5,1;5,7;3,2”  
=\> “5,1;5,7;3,2”  
irb\> s.match(re).captures  
=\> [“5”, “1”, “;3,2”, “3”, “2”]

You can wrap another set of parentheses in there:

irb\> re = /(\d+),(\d+)((;(\d+),(\d+))_)/  
=\> /(\d+),(\d+)((;(\d+),(\d+))_)/  
irb\> s.match(re).captures  
=\> [“5”, “1”, “;5,7;3,2”, “;3,2”, “3”, “2”]

But in addition to Andrew’s split, you could use String#scan

irb\> s.scan(/(\d+),(\d+)/)  
=\> [[“5”, “1”], [“5”, “7”], [“3”, “2”]]

It all depends on what you’re trying to accomplish.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![micktaiwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@micktaiwan](https://www.ruby-forum.com/u/micktaiwan)\
**Post date:** [January 28, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/regexp-multiple-matches/158817/7 "2009-01-28T17:26:36Z")

</div>

Sure, that’s what I’ve just done 🙂  
Just wondering.  
Thanks anyway.

Mickael.

Andrew T. wrote:

> On Wed, Jan 28, 2009 at 5:00 PM, Mickael Faivre-Macon  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > > > My question is why ;5,7 is not matched ?  
> > > > which  
> > > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> What about tackling it from a completely different angle
> 
> s = “5,1;5,7;3,2”  
> pairs = s.split(‘;’) #=\> [“5,1”, “5,7”, “3,2”]  
> pairs.each do |pair|  
> pair.split(‘,’) #=\> [“5”, “1”] etc  
> end
> 
> –  
> Andrew T.  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)  
> [http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)
> 
> “I have never let my schooling interfere with my education” - Mark Twain
