Group names in regular expressions

Discussion in 'Ruby' started by donn@cmscms.com, Nov 15, 2006.

  1. Guest

    Given the following regex to parse telephone numbers:

    /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
    )?(?<party>[0-9]{4})/

    Is there a regex.method to access the parse data by group name? I was
    hoping something like the following behavior:

    rx = /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
    )?(?<party>[0-9]{4})/
    md = rx.match('800 325-3535')
    puts md['area']+md['exch']+md['party']

    I'm not that skilled with regex (yet), so I don't know if I could
    extend the regex class with my own method. I'm not sure what all data
    is available to me after parsing.

    dvn
    , Nov 15, 2006
    #1
    1. Advertising

  2. wrote:
    > Given the following regex to parse telephone numbers:
    >
    > /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
    > )?(?<party>[0-9]{4})/
    >
    > Is there a regex.method to access the parse data by group name? I was
    > hoping something like the following behavior:
    >
    > rx = /^(\(?(?<area>[0-9]{3})\)?)?(\-| )?(?<exch>[0-9]{3})(\-|
    > )?(?<party>[0-9]{4})/
    > md = rx.match('800 325-3535')
    > puts md['area']+md['exch']+md['party']
    >
    > I'm not that skilled with regex (yet), so I don't know if I could
    > extend the regex class with my own method. I'm not sure what all data
    > is available to me after parsing.
    >
    > dvn


    With Ruby's builtin regex engine? No. With Oniguruma (or the Ruby 1.9
    branch)? Yes.

    http://www.geocities.jp/kosako3/oniguruma/

    Regards,

    Dan
    Daniel Berger, Nov 15, 2006
    #2
    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. Jay Douglas
    Replies:
    0
    Views:
    584
    Jay Douglas
    Aug 15, 2003
  2. James Collier
    Replies:
    4
    Views:
    1,065
    Erik Heneryd
    Aug 12, 2004
  3. Fredrik Lundh
    Replies:
    0
    Views:
    383
    Fredrik Lundh
    Aug 11, 2004
  4. Replies:
    3
    Views:
    572
    mlimber
    Aug 12, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    216
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page