accessing all matches using regex

Discussion in 'Ruby' started by Spike Grobstein, Aug 6, 2007.

  1. Hey all

    so, I primarily use PERL for creating shell scripts but I'm trying to
    learn ruby since it feels like it could do more of what I want with
    cleaner code. I especially like the way it has regex built right in.

    I'm running into an issue though, because in perl, I can do this:

    while (/href=\"([^\"]+)\"/) {
    ## do stuff...
    }

    I use this simple regex because it's based around what I use this style
    for the most.

    I can't figure out how to that in ruby. how can I iterate over all
    matches in a string? I could split the lines, but sometimes there may be
    two or more matches on a single line.

    is there a nice, clean, ruby way to do this?

    Thanks!
    --
    Posted via http://www.ruby-forum.com/.
     
    Spike Grobstein, Aug 6, 2007
    #1
    1. Advertising

  2. Spike Grobstein

    Jim Marshall Guest

    On 8/5/07, Spike Grobstein <> wrote:
    > while (/href=\"([^\"]+)\"/) {
    > ## do stuff...
    > }
    > ...
    > I can't figure out how to that in ruby. how can I iterate over all
    > matches in a string? I could split the lines, but sometimes there may be
    > two or more matches on a single line.
    >
    > is there a nice, clean, ruby way to do this?
    >


    Have a look at String#scan ("ri String#scan" might do it, depending
    upon your installation).
    For example,
    >some_string_with_html.scan(/href=\"([^\"]+)\"/) do |match|
    > do_stuff_with match
    >end
     
    Jim Marshall, Aug 6, 2007
    #2
    1. Advertising

  3. > is there a nice, clean, ruby way to do this?

    Have a look at String#scan
     
    Gordon Thiesfeld, Aug 6, 2007
    #3
  4. david karapetyan wrote:
    > look up gsub! for the String class. you can write
    >
    > some_string.gsub(/href=\"([^\"]+)\"/) do |match|
    > ## do stuff with match
    > end
    >
    > i'm pretty sure the above code will work for what you are trying to do.


    you know, I thought about String#scan, but for some reason didn't think
    it was what I was looking for, but it works sorta nicely. it doesn't
    behave exactly how I'd want... but the String#gsub method looks more
    like what I'm looking for; this way, I can loop and access my various
    $1, $2, etc matches and not have to work with an array that I have to
    loop over in a weird way (since I don't use some of matches).

    I'll have to play with this a little more.

    thanks!
    --
    Posted via http://www.ruby-forum.com/.
     
    Spike Grobstein, Aug 6, 2007
    #4
    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. Stephan Bour

    Extracting matches from Regex.Split

    Stephan Bour, Oct 29, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,569
    Stephan Bour
    Oct 30, 2003
  2. darrel
    Replies:
    1
    Views:
    802
    Blair Bonnett
    Jan 3, 2005
  3. Replies:
    3
    Views:
    1,567
  4. nadabadan

    using regex to select line matches

    nadabadan, Jun 15, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    136
    Gunnar Hjalmarsson
    Jun 15, 2007
  5. Roy Smith

    Finding all regex matches by index?

    Roy Smith, May 30, 2012, in forum: Python
    Replies:
    1
    Views:
    166
    Roy Smith
    May 30, 2012
Loading...

Share This Page