Regular Expresion Needed

M

Matt White

Hello,

I am fairly new to Ruby. I have some good Perl experience and I need
to "translate" a regex in Perl over to Ruby. Here's what I have:

$_ = 'abc123def456ghi789jkl';
while(m/(\d{4})/g)
{ print "$1\n"; }

Which outputs:
123
456
789

Ruby doesn't seem to have the same modifiers that Perl does (that
handy little g). How can I get this same output in Ruby? Thanks!
 
A

Alex Young

Matt said:
Hello,

I am fairly new to Ruby. I have some good Perl experience and I need
to "translate" a regex in Perl over to Ruby. Here's what I have:

$_ = 'abc123def456ghi789jkl';
while(m/(\d{4})/g)
{ print "$1\n"; }

Which outputs:
123
456
789

Ruby doesn't seem to have the same modifiers that Perl does (that
handy little g). How can I get this same output in Ruby? Thanks!

'abc123def456ghi789jkl'.scan(/\d{3}/).each{|m| puts m}
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 20. Jun 2007, 04:05:07 +0900 schrieb Matt White:
I am fairly new to Ruby. I have some good Perl experience and I need
to "translate" a regex in Perl over to Ruby. Here's what I have:

$_ = 'abc123def456ghi789jkl';
while(m/(\d{4})/g)
{ print "$1\n"; }

Which outputs:
123
456
789

'abc123def456ghi789jkl'.scan /\d{1,4}/ do |x| puts x end
'abc123def456ghi789jkl'.scan /\d{1,4}/ do puts $& end
'abc123def456ghi789jkl'.scan( /\d{1,4}/) { |x| puts x }
'abc123def456ghi789jkl'.scan( /\d{1,4}/) { puts $& }

Bertram
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top