End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
MatchData
MatchData encapsulates the result of matching a Regexp against string. It is returned by Regexp#match and String#match, and also stored in a global variable returned by Regexp.last_match.
Usage:
url = 'https://docs.ruby-lang.org/en/2.5.0/MatchData.html' m = url.match(/(\d\.?)+/) # => #<MatchData "2.5.0" 1:"0"> m.string # => "https://docs.ruby-lang.org/en/2.5.0/MatchData.html" m.regexp # => /(\d\.?)+/ # entire matched substring: m[0] # => "2.5.0" # Working with unnamed captures m = url.match(%r{([^/]+)/([^/]+)\.html$}) m.captures # => ["2.5.0", "MatchData"] m[1] # => "2.5.0" m.values_at(1, 2) # => ["2.5.0", "MatchData"] # Working with named captures m = url.match(%r{(?<version>[^/]+)/(?<module>[^/]+)\.html$}) m.captures # => ["2.5.0", "MatchData"] m.named_captures # => {"version"=>"2.5.0", "module"=>"MatchData"} m[:version] # => "2.5.0" m.values_at(:version, :module) # => ["2.5.0", "MatchData"] # Numerical indexes are working, too m[1] # => "2.5.0" m.values_at(1, 2) # => ["2.5.0", "MatchData"]
Global variables equivalence
Parts of last MatchData (returned by Regexp.last_match) are also aliased as global variables:
-
$~isRegexp.last_match; -
$&isRegexp.last_match[ 0 ]; -
$1,$2, and so on areRegexp.last_match[ i ](captures by number); -
$`isRegexp.last_match.pre_match; -
$'isRegexp.last_match.post_match; -
$+isRegexp.last_match[ -1 ](the last capture).
See also “Special global variables” section in Regexp documentation.
Instance methods 22
Ruby version changes 2
-
3.1
-
MatchData#matchis added [Feature #18172] -
MatchData#match_lengthis added [Feature #18172]
-