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?