[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Regular Expresion Needed

Matt White

6/19/2007 7:01:00 PM

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!

2 Answers

Alex Young

6/19/2007 7:30:00 PM

0

Matt White wrote:
> 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}

--
Alex

Bertram Scharpf

6/19/2007 7:36:00 PM

0

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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...