# Group names in regular expressions

**URL:** <https://www.ruby-forum.com/t/group-names-in-regular-expressions/76072>\
**Category:** Ruby\
**Created:** [November 15, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/group-names-in-regular-expressions/76072 "2006-11-15T18:22:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 15, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/group-names-in-regular-expressions/76072/1 "2006-11-15T18:22:57Z")

</div>

Given the following regex to parse telephone numbers:

/^((?(?[0-9]{3}))?)?(-| )?(?[0-9]{3})(-|  
)?(?[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 = /^((?(?[0-9]{3}))?)?(-| )?(?[0-9]{3})(-|  
)?(?[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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 15, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/group-names-in-regular-expressions/76072/2 "2006-11-15T20:05:20Z")

</div>

removed\_email\_address@domain.invalid wrote:

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

For “group names”, you can use this basic replacement idea:

[1,2,3,4].each do |exch|  
[“jones”,“smith”].each do |party|  
[303,404,505].each do |area|  
array = data.scan(%r{regex  
code(#{exch})(#{party})[regex  
code]})  
end  
end  
end

using this code and suitable data, “array” will contain:

[[area1,exch1,party1],[area2,exch2,party2], … ]

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 15, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/group-names-in-regular-expressions/76072/3 "2006-11-15T20:11:23Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 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/](http://www.geocities.jp/kosako3/oniguruma/)

Regards,

Dan
