RegExp question

J

Jens Riedel

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
 
B

Brian Schröder

Hello,
=20
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.
=20
How can I achieve this most simple?
=20
Example:
=20
'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.'
=20
Searching for /Example_\d/ should give me an array ("Example_1",
"Example_2", "Example_3")
=20
Greetings,
Jens
=20
=20

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)
=3D> ["Example_1", "Example_2", "Example_3"]

gr=FC=DFe,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
J

Jens Riedel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top