# Newby Ruby Regular Expression Question

**URL:** <https://www.ruby-forum.com/t/newby-ruby-regular-expression-question/140266>\
**Category:** Ruby\
**Created:** [June 12, 2008, 12:50pm UTC](https://www.ruby-forum.com/t/newby-ruby-regular-expression-question/140266 "2008-06-12T12:50:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruchira\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@Ruchira\_B](https://www.ruby-forum.com/u/Ruchira_B)\
**Post date:** [June 12, 2008, 12:50pm UTC](https://www.ruby-forum.com/t/newby-ruby-regular-expression-question/140266/1 "2008-06-12T12:50:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ruchira\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@Ruchira\_B](https://www.ruby-forum.com/u/Ruchira_B)\
**Post date:** [June 12, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/newby-ruby-regular-expression-question/140266/2 "2008-06-12T13:00:33Z")

</div>

On Thu, Jun 12, 2008 at 12:44 PM, Ruchira B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

As simple as possible, but not simpler.  
Albert Einstein

---

<div class="post-metadata">

**Author:** ![Ruchira\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@Ruchira\_B](https://www.ruby-forum.com/u/Ruchira_B)\
**Post date:** [June 12, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/newby-ruby-regular-expression-question/140266/3 "2008-06-12T13:30:38Z")

</div>

Hi –

On Thu, 12 Jun 2008, Ruchira B. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Ruchira\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@Ruchira\_B](https://www.ruby-forum.com/u/Ruchira_B)\
**Post date:** [June 12, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/newby-ruby-regular-expression-question/140266/4 "2008-06-12T15:28:26Z")

</div>

On Thu, Jun 12, 2008 at 1:29 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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/](http://ruby-smalltalk.blogspot.com/)

* * *

As simple as possible, but not simpler.  
Albert Einstein

---

<div class="post-metadata">

**Author:** ![Ruchira\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@Ruchira\_B](https://www.ruby-forum.com/u/Ruchira_B)\
**Post date:** [June 12, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/newby-ruby-regular-expression-question/140266/5 "2008-06-12T17:46:12Z")

</div>

On Jun 12, 6:26 pm, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Thu, Jun 12, 2008 at 1:29 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> \> 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/](http://ruby-smalltalk.blogspot.com/)
> 
> * * *
> 
> As simple as possible, but not simpler.  
> Albert Einstein

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
