Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

MatchData#names

mtch.names → [name1, name2, ...]

Returns a list of names of captures as an array of strings. It is same as mtch.regexp.names.

Example
/(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names
#=> ["foo", "bar", "baz"]

m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a" y:nil>
m.names                          #=> ["x", "y"]
See also #named_captures #offset #length #post_match also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1