[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

RegExp question

Jens Riedel

5/30/2005 8:16:00 AM

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
2 Answers

Brian Schröder

5/30/2005 8:31:00 AM

0

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

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

--
http://ruby.brian-sch...

Stringed instrument chords: http://chordlist.brian-sch...


Jens Riedel

5/30/2005 8:47:00 AM

0

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