BigBinary Blog

We write about Ruby on Rails, React.js, React Native, remote work, open source, engineering and design.

Ruby 2.4 Extracting captured data from Regexp results

This blog is part of our Ruby 2.4 series.

Ruby has MatchData type which is returned by Regexp#match and Regexp.last_match.

It has methods #names and #captures to return the names used for capturing and the actual captured data respectively.

1
2pattern = /(?<number>\d+) (?<word>\w+)/
3match_data = pattern.match('100 thousand')
4#=> #<MatchData "100 thousand" number:"100" word:"thousand">
5
6>> match_data.names
7=> ["number", "word"]
8>> match_data.captures
9=> ["100", "thousand"]
10

If we want all named captures in a key value pair, we have to combine the result of names and captures.

1
2match_data.names.zip(match_data.captures).to_h
3#=> {"number"=>"100", "word"=>"thousand"}
4

Ruby 2.4 adds #named_captures which returns both the name and data of the capture groups.

1
2pattern=/(?<number>\d+) (?<word>\w+)/
3match_data = pattern.match('100 thousand')
4
5match_data.named_captures
6#=> {"number"=>"100", "word"=>"thousand"}
7
Rohit Kumar in Ruby 2.4
November 10, 2016
Share

Subscribe to our newsletter