12

I was reading a regex group matching question and I see that there are two ways to reference capture groups from a regex expression, namely,

  1. Match string method e.g. string.match(/(^.*)(:)(.*)/i).captures
  2. Perl-esque capture group variables such as $1, $2, etc obtained from if match =~ /(^.*)(:)(.*)/i
  3. Update: As mentioned by 0xCAFEBABE there is a third option too - the last_match method

Which is better? With 1), for safety, you would have to use an if statement to guard against nils so why not just extract the information then? Instead of a second step calling the string captures method. So option 2) looks more convenient to me.

3
  • 2
    There's even a third one: RegExp.last_match. Oh well, timtowtdi.
    – 0xCAFEBABE
    Jul 19, 2013 at 10:43
  • There is no Match class. You probably mean MatchData class.
    – sawa
    Jul 19, 2013 at 11:15
  • @sawa My bad, it's a string method, I have updated the question. Jul 19, 2013 at 12:26

3 Answers 3

22

Since v2.4.6, Ruby has had named_captures, which can be used like this. Just add the ?<some_name> syntax inside a capture group.

/(\w)(\w)/.match("ab").captures # => ["a", "b"]
/(\w)(\w)/.match("ab").named_captures # => {}

/(?<some_name>\w)(\w)/.match("ab").captures # => ["a"]
/(?<some_name>\w)(\w)/.match("ab").named_captures # => {"some_name"=>"a"}

Even more relevant, you can reference a named capture by name!

result = /(?<some_name>\w)(\w)/.match("ab")
result["some_name"] # => "a" 
2
  • 1
    For modern Ruby, circa 2020, this is the correct answer.
    – moveson
    Jul 29, 2020 at 21:19
  • 1
    You can also call .match on a string and pass in a regexp, the reverse of the answer here.
    – smoyth
    Jul 19 at 11:53
4

For simple tasks, directly accessing the pseudo variables $1, etc. may be short and easier, but when things get complicated, accessing things via MatchData instances is (nearly) the only way to go.

For example, suppose you are doing nested gsub:

string1.gsub(regex1) do |string2|
  string2.gsub(regex2) do
    ... # Impossible/difficult to refer to match data of outer loop
  end
end

Within the inner loop, suppose you wanted to refer to a captured group of the outer gsub. Calling $1, $2, etc. would not give the right result because the last match data has changed by doing the inner gsub loop. This will be a source of bug.

It is necessary to refer to captured groups via match data:

string1.gsub(regex1) do |string2|
  m1 = $~
  string2.gsub(regex2) do
    m2 = $~
    ... # match data of the outer loop can be accessed via `m1`.
        # match data of the inner loop can be accessed via `m2`.
  end
end

In short, if you want to do short hackish things for simple tasks, you can use the pseudo variables. If you want to keep your code more structured and expandable, you should access data through match data.

0

From Ruby 3.3.0 symbolize_names: true option is available for MatchData#named_captures

If keyword argument symbolize_names is given a true value, the keys in the resulting hash are Symbols

m = /(?<a>.)(?<b>.)/.match("01") #=> #<MatchData "01" a:"0" b:"1">

m.named_captures #=> {"a" => "0", "b" => "1"}
m.named_captures(symbolize_names: true) #=> {:a => "0", :b => "1"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.