[newbie] How do I match a regex multiple times?

Discussion in 'Ruby' started by Robo, Apr 3, 2004.

  1. Robo

    Robo Guest

    When I use String#match, it only seems to match the regex pattern once. How
    do I tell it to match the pattern as many times as possible?

    e.g. puts 'robo ruler ruby'.match(/r\w+/)

    That only return 'robo', but I would like it to return [robo, ruler, ruby]

    I tried using the /g modifier, but Ruby doesn't seem to know about that.

    I'm doing this 'cos I'm trying to extract all the function names from a PHP
    file, but I shouldn't have to tell it to search it line by line do I?

    Robo
     
    Robo, Apr 3, 2004
    #1
    1. Advertising

  2. Robo

    ts Guest

    >>>>> "R" == Robo <> writes:

    R> e.g. puts 'robo ruler ruby'.match(/r\w+/)

    String#scan

    svg% ri String#scan
    ------------------------------------------------------------ String#scan
    str.scan(pattern) => array
    str.scan(pattern) {|match, ...| block } => str
    ------------------------------------------------------------------------
    Both forms iterate through _str_, matching the pattern (which may
    be a +Regexp+ or a +String+). For each match, a result is generated
    and either added to the result array or passed to the block. If the
    pattern contains no groups, each individual result consists of the
    matched string, +$&+. If the pattern contains groups, each
    individual result is itself an array containing one entry per
    group.

    a = "cruel world"
    a.scan(/\w+/) #=> ["cruel", "world"]
    a.scan(/.../) #=> ["cru", "el ", "wor"]
    a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
    a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]

    And the block form:

    a.scan(/\w+/) {|w| print "<<#{w}>> " }
    print "\n"
    a.scan(/(.)(.)/) {|a,b| print b, a }
    print "\n"

    _produces:_

    <<cruel>> <<world>>
    rceu lowlr

    svg%


    Guy Decoux
     
    ts, Apr 3, 2004
    #2
    1. Advertising

  3. Robo

    Bil Kleb Guest

    Robo wrote:
    > When I use String#match, it only seems to match the regex pattern once. How
    > do I tell it to match the pattern as many times as possible?


    puts 'robo ruler ruby'.scan(/r\w+/)
    robo
    ruler
    ruby

    Regards,
    --
    Bil Kleb, Hampton, Virginia
     
    Bil Kleb, Apr 3, 2004
    #3
    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. hiwa
    Replies:
    0
    Views:
    657
  2. Jeff Epler
    Replies:
    2
    Views:
    292
    Avraham Makeler
    Apr 21, 2004
  3. Jack Steven
    Replies:
    2
    Views:
    476
    Chris Rebert
    Mar 9, 2009
  4. Markus Fischer
    Replies:
    9
    Views:
    182
    7stud --
    Apr 8, 2011
  5. David Karr
    Replies:
    4
    Views:
    217
    Ben Bacarisse
    Sep 16, 2013
Loading...

Share This Page