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. Advertisements

  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. Advertisements

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. Paddy McCarthy
    Replies:
    3
    Views:
    941
    Anthony J Bybell
    Sep 24, 2004
  2. Jay Douglas
    Replies:
    0
    Views:
    782
    Jay Douglas
    Aug 15, 2003
  3. Bob
    Replies:
    1
    Views:
    541
    Lucas Tam
    Jul 30, 2004
  4. Lewis G. Pringle, Jr.
    Replies:
    0
    Views:
    746
    Lewis G. Pringle, Jr.
    Sep 30, 2003
  5. Craig
    Replies:
    0
    Views:
    593
    Craig
    Feb 9, 2004
  6. James Collier
    Replies:
    4
    Views:
    1,301
    Erik Heneryd
    Aug 12, 2004
  7. Replies:
    3
    Views:
    745
    mlimber
    Aug 12, 2008
  8. Noman Shapiro
    Replies:
    0
    Views:
    421
    Noman Shapiro
    Jul 17, 2013
Loading...