# Named backreferences with Oniguruma

**URL:** <https://www.ruby-forum.com/t/named-backreferences-with-oniguruma/100172>\
**Category:** Ruby\
**Created:** [July 27, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/named-backreferences-with-oniguruma/100172 "2006-07-27T15:13:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 27, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/named-backreferences-with-oniguruma/100172/1 "2006-07-27T15:13:21Z")

</div>

Hi all,

Ruby 1.8.4  
Oniguruma 2.5.6

I’m trying to figure out how to get at named backreferences with  
Oniguruma, but I’m having some problems figuring it out. Here’s the  
interface I would _like_ to have:

# pseudo.rb

str = “hello world”  
reg = /^(?\w+?)\s(?\w+)$/

match = reg.match(str)

first = match[:first]  
last = match[:last]

But this doesn’t work - I mean, the regex works, but the way I’m trying  
to get at the backreferences does not. Nor does there appear to be  
anything like Python’s MatchData#groupdict() method (which returns a  
hash).

How do I get at the named backreferences from the MatchData object? Is  
there a way?

Thanks,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 27, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/named-backreferences-with-oniguruma/100172/2 "2006-07-27T15:23:17Z")

</div>

> > > > > “B” == Berger, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

B\> reg = /^(?\w+?)\s(?\w+)$/

moulon% cat b.rb  
#!/usr/bin/ruby  
str = “hello world”  
reg = /^(?\w+?)\s(?\w+)$/

match = reg.match(str)

first = match[:first]  
last = match[:last]

p first,last  
moulon%

moulon% ruby -v b.rb  
ruby 1.9.0 (2006-07-14) [i686-linux]  
“hello”  
“world”  
moulon%

moulon% grep ONIGURUMA\_VERSION ruby/oniguruma.h  
#define ONIGURUMA\_VERSION\_MAJOR 4  
#define ONIGURUMA\_VERSION\_MINOR 0  
#define ONIGURUMA\_VERSION\_TEENY 3  
moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 27, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/named-backreferences-with-oniguruma/100172/3 "2006-07-27T15:33:48Z")

</div>

Hi,

At Thu, 27 Jul 2006 22:11:02 +0900,  
Berger, Daniel wrote in [ruby-talk:204263]:

> Ruby 1.8.4  
> Oniguruma 2.5.6

Not 1.9? That combination isn’t supported officially.

> I’m trying to figure out how to get at named backreferences with  
> Oniguruma, but I’m having some problems figuring it out. Here’s the  
> interface I would _like_ to have:

MatchData#[] in 1.8 doesn’t take symbols.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 27, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/named-backreferences-with-oniguruma/100172/4 "2006-07-27T16:17:15Z")

</div>

Hi,

At Thu, 27 Jul 2006 22:35:18 +0900,  
Daniel B. wrote in [ruby-talk:204267]:

> > > I’m trying to figure out how to get at named backreferences with  
> > > Oniguruma, but I’m having some problems figuring it out. Here’s the  
> > > interface I would _like_ to have:
> > 
> > MatchData#[] in 1.8 doesn’t take symbols.
> 
> Couldn’t it be added? I mean, it’s a custom re.c anyway, isn’t it?

To which? Oniguruma doesn’t contain the patch for 1.8?

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 27, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/named-backreferences-with-oniguruma/100172/5 "2006-07-27T15:37:09Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi,
> 
> At Thu, 27 Jul 2006 22:11:02 +0900,  
> Berger, Daniel wrote in [ruby-talk:204263]:
> 
> > Ruby 1.8.4  
> > Oniguruma 2.5.6
> 
> Not 1.9? That combination isn’t supported officially.

Oh, I didn’t know that.

> > I’m trying to figure out how to get at named backreferences with  
> > Oniguruma, but I’m having some problems figuring it out. Here’s the  
> > interface I would _like_ to have:
> 
> MatchData#[] in 1.8 doesn’t take symbols.

Couldn’t it be added? I mean, it’s a custom re.c anyway, isn’t it?

Thanks,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.
