# String extraction using RegExp

**URL:** <https://www.ruby-forum.com/t/string-extraction-using-regexp/139938>\
**Category:** Ruby\
**Created:** [June 9, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/string-extraction-using-regexp/139938 "2008-06-09T12:05:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![upgundecha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9de053/32.png) [@upgundecha](https://www.ruby-forum.com/u/upgundecha)\
**Post date:** [June 9, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/string-extraction-using-regexp/139938/1 "2008-06-09T12:05:41Z")

</div>

Hi,

I want to extract “New User Registration” and “New User” from a string  
as follows. I am using RegExp for this.

r1 = Regexp.new(’("([^.]\*)")’)  
matches = r1.match(‘Page(“New User Registration”).Frame(“New User”)’)

puts !matches.nil?  
puts matches[0]  
puts matches[1]  
puts matches[2]

However above code returns only “New User Registration”. Is there any  
way to find the next match. Is something missing in this code?

Please advice.

Thanks in advance,  
Unmesh

---

<div class="post-metadata">

**Author:** ![upgundecha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9de053/32.png) [@upgundecha](https://www.ruby-forum.com/u/upgundecha)\
**Post date:** [June 9, 2008, 12:17pm UTC](https://www.ruby-forum.com/t/string-extraction-using-regexp/139938/2 "2008-06-09T12:17:40Z")

</div>

On Monday 09 June 2008, Unmesh Gundecha wrote:

> puts matches[1]  
> puts matches[2]
> 
> However above code returns only “New User Registration”. Is there any  
> way to find the next match. Is something missing in this code?
> 
> Please advice.
> 
> Thanks in advance,  
> Unmesh

Regexp.match and String.match only return the first match. If you want  
to get  
all matches, you can use either String#scan or class StringScanner. In  
your  
case, I think the better choice is the former:

str = ‘Page(“New User Registration”).Frame(“New User”)’  
str.scan(r1)  
=\> [[“New User Registration”], [“New User”]]

String#scan scans the string for all matches and returns an array of  
arrays.  
Each subarray contains all the groups for the corresponding match (if  
the  
regexp doesn’t contain groups, then String#scan returns an array of  
strings,  
each of which is the matched text).

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![upgundecha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9de053/32.png) [@upgundecha](https://www.ruby-forum.com/u/upgundecha)\
**Post date:** [June 9, 2008, 12:51pm UTC](https://www.ruby-forum.com/t/string-extraction-using-regexp/139938/3 "2008-06-09T12:51:37Z")

</div>

Stefano C. wrote:

> Regexp.match and String.match only return the first match. If you want  
> to get  
> all matches, you can use either String#scan or class StringScanner. In  
> your  
> case, I think the better choice is the former:
> 
> str = ‘Page(“New User Registration”).Frame(“New User”)’  
> str.scan(r1)  
> =\> [[“New User Registration”], [“New User”]]
> 
> String#scan scans the string for all matches and returns an array of  
> arrays.  
> Each subarray contains all the groups for the corresponding match (if  
> the  
> regexp doesn’t contain groups, then String#scan returns an array of  
> strings,  
> each of which is the matched text).
> 
> I hope this helps
> 
> Stefano

Thanks Stefano,

String#scan worked for me.

Cheers!!  
Unmesh
