Skip to content

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

instance method

Regexp#named_captures

named_captures → hash

Returns a hash representing named captures of self (see Named Captures):

  • Each key is the name of a named capture.

  • Each value is an array of integer indexes for that named capture.

Examples:

Example
/(?<foo>.)(?<bar>.)/.named_captures # => {"foo"=>[1], "bar"=>[2]}
/(?<foo>.)(?<foo>.)/.named_captures # => {"foo"=>[1, 2]}
/(.)(.)/.named_captures             # => {}
See also #match? #names #match #options also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0