Newby Ruby Regular Expression Question

Discussion in 'Ruby' started by Ruchira Bomiriya, Jun 12, 2008.

  1. 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
    Ruchira Bomiriya, Jun 12, 2008
    #1
    1. Advertising

  2. Ruchira Bomiriya

    Robert Dober Guest

    On Thu, Jun 12, 2008 at 12:44 PM, Ruchira Bomiriya
    <> wrote:
    > 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



    --
    http://ruby-smalltalk.blogspot.com/

    ---
    As simple as possible, but not simpler.
    Albert Einstein
    Robert Dober, Jun 12, 2008
    #2
    1. Advertising

  3. Hi --

    On Thu, 12 Jun 2008, Ruchira Bomiriya wrote:

    > 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!
    David A. Black, Jun 12, 2008
    #3
  4. Ruchira Bomiriya

    Robert Dober Guest

    On Thu, Jun 12, 2008 at 1:29 PM, David A. Black <> wrote:
    <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/

    ---
    As simple as possible, but not simpler.
    Albert Einstein
    Robert Dober, Jun 12, 2008
    #4
  5. On Jun 12, 6:26 pm, Robert Dober <> wrote:
    > On Thu, Jun 12, 2008 at 1:29 PM, David A. Black <> wrote:
    > <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/
    >
    > ---
    > 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
    Ruchira Bomiriya, Jun 12, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,272
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    639
    Iain Norman
    Feb 16, 2005
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    831
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,923
    vforvikash
    Dec 29, 2008
  5. rj-cole
    Replies:
    2
    Views:
    110
Loading...

Share This Page