Newby Ruby Regular Expression Question

R

Ruchira Bomiriya

Dear Expert,

I have recently started Ruby programming. I have used Perl before and
liked it mostly due to the power of regular expressions. But Ruby's
ability scale and modularize is absolutely amazing.

I want to find FOUR instances of A in the string "AaAbAcAd" using
regular expressions.

I tried

str = "AaAbAcAd"
md = str.match(/(A)/)

and get just one instance (seems like the last 'A')

The perl equivalent of what I really want is

$str = "AaAbAcAd"
@md = ($str =~ /A/g)

So what I really want to find out is if there is something equivalent
to the Perl g option available in Ruby. How would you get all FOUR
matches in a reasonable way?

Thanks in advance.
Ruchira
 
R

Robert Dober

Dear Expert,

I have recently started Ruby programming. I have used Perl before and
liked it mostly due to the power of regular expressions. But Ruby's
ability scale and modularize is absolutely amazing.

I want to find FOUR instances of A in the string "AaAbAcAd" using
regular expressions.

I tried


and get just one instance (seems like the last 'A')

The perl equivalent of what I really want is

$str = "AaAbAcAd"
@md = ($str =~ /A/g)

maybe String#scan does the same, sorry my perl times are too remote

str = "AaAbAcAd"
md = str.scan /A/

HTH
Robert
 
D

David A. Black

Hi --

Dear Expert,

I have recently started Ruby programming. I have used Perl before and
liked it mostly due to the power of regular expressions. But Ruby's
ability scale and modularize is absolutely amazing.

I want to find FOUR instances of A in the string "AaAbAcAd" using
regular expressions.

I tried

str = "AaAbAcAd"
md = str.match(/(A)/)

and get just one instance (seems like the last 'A')

The perl equivalent of what I really want is

$str = "AaAbAcAd"
@md = ($str =~ /A/g)

So what I really want to find out is if there is something equivalent
to the Perl g option available in Ruby. How would you get all FOUR
matches in a reasonable way?

str.scan(/A/). I can't help wondering though... what made you think
that the A you got is the last A? (It's actually the first A.)


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!
 
R

Ruchira Bomiriya

<snip>> str.scan(/A/).

Hey it does not only happen to Rick, it happens to me too;)
Does this mean I am one of the best?
--http://ruby-smalltalk.blogspot.com/

Thanks Robert, str.scan(/A/) does what I wanted.

Sorry for the confusion with the last A issue. I was messing with
various regex patterns and some of them were greedy enough, took some
of my brain cells away and made me think stupid things.

But str.scan(/A/) has made me regain my faith in ruby's regular
expressions. Not that I was particularly skeptic, but was merely
stuck!

Thanks again.
Ruchira
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top