I am having trouble with named captures in regular expressions in Ruby 2.0. I have a string variable and an interpolated regular expression:
str = "hello world"
re = /\w+/
/(?<greeting>#{re})/ =~ str
greeting
It raises the following exception:
prova.rb:4:in
<main>': undefined local variable or method
greeting' for main:Object (NameError)
shell returned 1
However, the interpolated expression works without named captures. For example:
/(#{re})/ =~ str
$1
# => "hello"
As an addendum to both answers in order to make it crystal clear:
Interpolation is fine with named captures, just use the MatchData object, most easily returned via
match
.Named Captures Must Use Literals
You are encountering some limitations of Ruby's regular expression library. The Regexp#=~ method limits named captures as follows:
#{}
, also disables the assignment.You'll need to decide whether you want named captures or interpolation in your regular expressions. You currently cannot have both.
Assign the result of
#match
; this will be accessible as a hash that allows you to look up your named capture groups:Alternately, give
#match
a block, which will receive the match results: