# Multiple matches

**URL:** <https://www.ruby-forum.com/t/multiple-matches/169025>\
**Category:** Ruby\
**Created:** [May 28, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/multiple-matches/169025 "2009-05-28T15:34:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [May 28, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/multiple-matches/169025/1 "2009-05-28T15:34:48Z")

</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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [May 28, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/multiple-matches/169025/2 "2009-05-28T16:16:51Z")

</div>

Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [May 28, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/multiple-matches/169025/3 "2009-05-28T16:52:45Z")

</div>

On May 28, 2009, at 10:15 AM, matt neuburg wrote:

> The last regex match is always hanging around waiting for you to ask  
> about it… Hope that helps - m.
> 
> –  
> matt neuburg, phd = removed\_email\_address@domain.invalid, [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)

irb\> s  
=\> “ccccatgggctacatgggtcat”  
irb\> pos = []  
=\> []  
irb\> while x = s.index(‘cat’, (pos.last||-1)+1)  
irb\> pos \<\< x  
irb\> end  
=\> nil  
irb\> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [May 28, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/multiple-matches/169025/4 "2009-05-28T17:49:06Z")

</div>

On Thu, May 28, 2009 at 9:34 PM, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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)  
> if n  
> [n] + indexes( str, n+=1)  
> else  
> []  
> 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
