Search

Refactoring Regular Expressions with Ruby 1.9 Named Captures

Brandon Beacher

1 min read

Dec 13, 2011

Refactoring Regular Expressions with Ruby 1.9 Named Captures

I’ve often felt like Ruby Regexp captures are a bit clumsy.

Let’s say we need to break apart phone numbers:

After executing this match, we might do something like this with the parsed number:

What’s up with the dollar signs and the sequential numbers?

I feel like I’m writing assembly code and referring to registers or memory offsets or something.

If I’m a new Ruby programmer reading this code, I might have no idea what is going on here.

We can do better if we upgrade from magical variables to the Regexp.last_match method:

At least this is a bit more readable than the magic variables.

And it’s probably easier for a newcomer to find documentation for Regexp.last_match than $1.

But there’s an even better way in Ruby 1.9 – named captures:

Now we’ve got readable code, and better documentation built into our regular expression.

Are you using Ruby 1.9 yet? If so, have you had a chance to use named captures?

Zack Simon

Reviewer Big Nerd Ranch

Zack is an Experience Director on the Big Nerd Ranch design team and has worked on products for companies ranging from startups to Fortune 100s. Zack is passionate about customer experience strategy, helping designers grow in their career, and sharpening consulting and delivery practices.

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Not applicable? Click here to schedule a call.

Stay in Touch WITH Big Nerd Ranch News