The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Named backreferences

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
Named backreferences Posted: Jul 26, 2006 8:14 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Daniel Berger.
Original Post: Named backreferences
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
Normally you might have some code with a regex like this:
str = "hello world"
reg = /^(\w+?)\s(\w+)$/

first = $1
last  = $2

Oh, sure, you might just use MatchData#captures instead of the globals. The point is you're referring to them by index, in one form or another. What if you could refer to them by name instead?
# pseudo.rb
str = "hello world"
reg = /^(?<first>\w+?)\s(?<last>\w+)$/

match = reg.match(str)

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

Python (though not Perl afaik, surprisingly) already has this ability in the form of the groupdict() method for its MatchData object:
>>> import re
>>> re.compile('a(?P<mid>.*)c').match('abc').groupdict()

{'mid': 'b'}

I've been playing with Oniguruma recently, trying to get symbolic group names to work. I was hoping that it would be something as simple as the pseudo.rb I pasted above. Unfortunately, that doesn't work. There's no MatchData#group method that returns a hash, either. In fact, I don't see how to refer back to it at all.

Anyway, assuming we do get a way to access named references, that will be pretty cool.

Read: Named backreferences

Topic: Look ma! I'm on Amazon! Previous Topic   Next Topic Topic: DSLs Are the Long Tail of Programming Languages

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use