End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
MatchData#names
mtch.names → [name1, name2, ...]
re.c
View on GitHub ↗
static VALUE
match_names(VALUE match)
{
match_check(match);
if (NIL_P(RMATCH(match)->regexp))
return rb_ary_new_capa(0);
return rb_reg_names(RMATCH(match)->regexp);
}
Returns a list of names of captures as an array of strings. This is the 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
#match_length
#post_match
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0