# RegExp question

**URL:** <https://rubytalk.org/t/regexp-question/18679>\
**Category:** ruby-talk\
**Created:** [30 May 2005 08:20 UTC](https://rubytalk.org/t/regexp-question/18679 "2005-05-30T08:20:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jens\_Riedel](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@Jens\_Riedel](https://rubytalk.org/u/Jens_Riedel)\
**Post date:** [30 May 2005 08:20 UTC](https://rubytalk.org/t/regexp-question/18679/1 "2005-05-30T08:20:21Z")

</div>

Hello,

I need to search a text for a specific pattern, then put all matches into an array.  
Usually I did this while going through the text line by line, but this time there can be more than one match per line.

How can I achieve this most simple?

Example:

'This is Example\_1 in a line in a long text with Example\_2 in the  
same line and Example\_3 in the next one.'

Searching for /Example\_\d/ should give me an array ("Example\_1", "Example\_2", "Example\_3")

Greetings,  
Jens

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [30 May 2005 08:31 UTC](https://rubytalk.org/t/regexp-question/18679/2 "2005-05-30T08:31:23Z")

</div>

Hallo Jens,

irb(main):001:0\> 'This is Example\_1 in a line in a long text with  
Example\_2 in the  
irb(main):002:0' same line and Example\_3 in the next one.'.scan(/Example\_\d+/m)  
=\> ["Example\_1", "Example\_2", "Example\_3"]

grüße,

Brian

> **···**
>
> On 30/05/05, Jens Riedel \<JensRie@gmx.de\> wrote:
> 
> > Hello,
> > 
> > I need to search a text for a specific pattern, then put all matches  
> > into an array.  
> > Usually I did this while going through the text line by line, but this  
> > time there can be more than one match per line.
> > 
> > How can I achieve this most simple?
> > 
> > Example:
> > 
> > 'This is Example\_1 in a line in a long text with Example\_2 in the  
> > same line and Example\_3 in the next one.'
> > 
> > Searching for /Example\_\d/ should give me an array ("Example\_1",  
> > "Example\_2", "Example\_3")
> > 
> > Greetings,  
> > Jens
> 
> --  
> [http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)
> 
> Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![Jens\_Riedel](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@Jens\_Riedel](https://rubytalk.org/u/Jens_Riedel)\
**Post date:** [30 May 2005 08:50 UTC](https://rubytalk.org/t/regexp-question/18679/3 "2005-05-30T08:50:21Z")

</div>

Hallo Brian,

> irb(main):001:0\> 'This is Example\_1 in a line in a long text with  
> Example\_2 in the  
> irb(main):002:0' same line and Example\_3 in the next one.'.scan(/Example\_\d+/m)  
> =\> ["Example\_1", "Example\_2", "Example\_3"]

vielen Dank, scan() hatte ich noch gar nicht entdeckt.

Grüße,  
Jens
