# Multiple matches

**URL:** <https://rubytalk.org/t/multiple-matches/53622>\
**Category:** ruby-talk\
**Created:** [28 May 2009 13:34 UTC](https://rubytalk.org/t/multiple-matches/53622 "2009-05-28T13:34:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [28 May 2009 13:34 UTC](https://rubytalk.org/t/multiple-matches/53622/1 "2009-05-28T13:34:55Z")

</div>

Hi all,

How to find out pattern'cat' in this string='ccccatgggctacatgggtcat'  
? There are three 'cat' within the string and I want to return the  
position of each match.

Although # match return the match , #pre\_match and #post\_match do the  
tricks but they only work for the first match. How about the 2nd, third  
match, and more? I try #scan(pattern) but it doesn't return the  
position for each match. I wonder how other handle this problem.

Thanks,

Li

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Matt9](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Matt9](https://rubytalk.org/u/Matt9)\
**Post date:** [28 May 2009 14:15 UTC](https://rubytalk.org/t/multiple-matches/53622/2 "2009-05-28T14:15:10Z")

</div>

It doesn't "return" the position but it does capture the position.

s = 'ccccatgggctacatgggtcat'  
s.scan('cat') {puts Regexp.last\_match.begin(0)}

The last regex match is always hanging around waiting for you to ask  
about it.... Hope that helps - m.

> **···**
>
> Li Chen \<chen\_li3@yahoo.com\> wrote:
> 
> > I try #scan(pattern) but it doesn't return the  
> > position for each match. I wonder how other handle this problem.
> 
> --  
> matt neuburg, phd = matt@tidbits.com, [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/)  
> Leopard - [http://www.takecontrolbooks.com/leopard-customizing.html](http://www.takecontrolbooks.com/leopard-customizing.html)  
> AppleScript - [http://www.amazon.com/gp/product/0596102119](http://www.amazon.com/gp/product/0596102119)  
> Read TidBITS! It's free and smart. [http://www.tidbits.com](http://www.tidbits.com)

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [28 May 2009 15:48 UTC](https://rubytalk.org/t/multiple-matches/53622/3 "2009-05-28T15:48:05Z")

</div>

> How to find out pattern'cat' in this string='ccccatgggctacatgggtcat'  
> ? There are three 'cat' within the string and I want to return the position of each match.

there is String#index.  
there is no indexes, but you can build one if you like

eg,

> s

=\> "ccccatgggctacatgggtcat"

> class String  
> def indexes str, n=0  
> n=index( str, n)  
> &nbsp;&nbsp;if n  
> &nbsp;&nbsp;&nbsp;&nbsp;[n] + indexes( str, n+=1)  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;end  
> end  
> end

=\> nil

> s.indexes "cat"

=\> [3, 12, 19]

the faster way is to loop, but i just want to test my recursion skill 😉

best regards  
-botp

> **···**
>
> On Thu, May 28, 2009 at 9:34 PM, Li Chen \<chen\_li3@yahoo.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [28 May 2009 14:51 UTC](https://rubytalk.org/t/multiple-matches/53622/4 "2009-05-28T14:51:31Z")

</div>

> > I try #scan(pattern) but it doesn't return the  
> > position for each match. I wonder how other handle this problem.
> 
> It doesn't "return" the position but it does capture the position.
> 
> s = 'ccccatgggctacatgggtcat'  
> s.scan('cat') {puts Regexp.last\_match.begin(0)}
> 
> The last regex match is always hanging around waiting for you to ask  
> about it.... Hope that helps - m.
> 
> -- matt neuburg, phd = matt@tidbits.com, [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/)  
> Leopard - [http://www.takecontrolbooks.com/leopard-customizing.html](http://www.takecontrolbooks.com/leopard-customizing.html)  
> AppleScript - [http://www.amazon.com/gp/product/0596102119](http://www.amazon.com/gp/product/0596102119)  
> Read TidBITS! It's free and smart. [http://www.tidbits.com](http://www.tidbits.com)

> s

=\> "ccccatgggctacatgggtcat"

> pos =

=\>

> while x = s.index('cat', (pos.last||-1)+1)  
> &nbsp;&nbsp;pos \<\< x  
> &nbsp;&nbsp;end

=\> nil

> pos

=\> [3, 12, 19]

String#index is the way to go. The second arg is the position to start looking for a match. .last==nil so it is a small trick to make the first loop test pass a 0 or 1 more than the previous match position.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On May 28, 2009, at 10:15 AM, matt neuburg wrote:
> 
> > Li Chen \<chen\_li3@yahoo.com\> wrote:
