Group names in regular expressions

D

donn

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
 
D

Daniel Berger

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top