Regex::MatchData
is the type of the special variable $~
, and is the type returned by Regex#match
and String#match
. It encapsulates all the results of a regular expression match.
if md = "Crystal".match(/[p-s]/) md.string # => "Crystal" md[0] # => "r" md[1]? # => nil end
Many Regex::MatchData
methods deal with capture groups, and accept an integer argument to select the desired capture group. Capture groups are numbered starting from 1
, so that 0
can be used to refer to the entire regular expression without needing to capture it explicitly.
Returns the match of the capture group named by group_name, or raises an KeyError
if there is no such named capture group.
Returns the match of the nth capture group, or raises an IndexError
if there is no nth capture group.
Returns the match of the capture group named by group_name, or nil
if there is no such named capture group.
Returns the match of the nth capture group, or nil
if there isn't an nth capture group.
Returns the position of the first character of the nth match.
Returns the position of the first byte of the nth match.
Returns the position of the next byte after the match.
Returns an array of unnamed capture groups.
Returns a shallow copy of this object.
Returns the position of the next character after the match.
Returns the number of capture groups, including named capture groups.
Appends this struct's name and instance variables names and values to the given IO.
Returns a hash of named capture groups.
Returns the part of the original string after the match.
Returns the part of the original string before the match.
Returns the original regular expression.
Returns the number of elements in this match object.
Returns the original string.
Convert this match data into an array.
Convert this match data into a hash.
Same as #inspect(io)
.
Struct
Value
Object
Object
Returns the match of the capture group named by group_name, or raises an KeyError
if there is no such named capture group.
"Crystal".match(/r(?<ok>ys)/).not_nil!["ok"] # => "ys" "Crystal".match(/r(?<ok>ys)/).not_nil!["ng"] # raises KeyError
When there are capture groups having same name, it returns the last matched capture group.
"Crystal".match(/(?<ok>Cr).*(?<ok>al)/).not_nil!["ok"] # => "al"
Returns the match of the nth capture group, or raises an IndexError
if there is no nth capture group.
"Crystal".match(/r(ys)/).not_nil![1] # => "ys" "Crystal".match(/r(ys)/).not_nil![2] # raises IndexError
Returns the match of the capture group named by group_name, or nil
if there is no such named capture group.
"Crystal".match(/r(?<ok>ys)/).not_nil!["ok"]? # => "ys" "Crystal".match(/r(?<ok>ys)/).not_nil!["ng"]? # => nil
When there are capture groups having same name, it returns the last matched capture group.
"Crystal".match(/(?<ok>Cr).*(?<ok>al)/).not_nil!["ok"]? # => "al"
Returns the match of the nth capture group, or nil
if there isn't an nth capture group.
When n is 0
, returns the match for the entire Regex
.
"Crystal".match(/r(ys)/).not_nil![0]? # => "rys" "Crystal".match(/r(ys)/).not_nil![1]? # => "ys" "Crystal".match(/r(ys)/).not_nil![2]? # => nil
Returns the position of the first character of the nth match.
When n is 0
or not given, uses the match of the entire Regex
. Otherwise, uses the match of the nth capture group.
"Crystal".match(/r/).not_nil!.begin(0) # => 1 "Crystal".match(/r(ys)/).not_nil!.begin(1) # => 2 "クリスタル".match(/リ(ス)/).not_nil!.begin(0) # => 1
Returns the position of the first byte of the nth match.
When n is 0
or not given, uses the match of the entire Regex
. Otherwise, uses the match of the nth capture group.
"Crystal".match(/r/).not_nil!.byte_begin(0) # => 1 "Crystal".match(/r(ys)/).not_nil!.byte_begin(1) # => 2 "クリスタル".match(/リ(ス)/).not_nil!.byte_begin(0) # => 3
Returns the position of the next byte after the match.
When n is 0
or not given, uses the match of the entire Regex
. Otherwise, uses the match of the nth capture group.
"Crystal".match(/r/).not_nil!.byte_end(0) # => 2 "Crystal".match(/r(ys)/).not_nil!.byte_end(1) # => 4 "クリスタル".match(/リ(ス)/).not_nil!.byte_end(0) # => 9
Returns an array of unnamed capture groups.
It is a difference from #to_a
that the result array does not contain the match for the entire Regex
(self[0]
).
match = "Crystal".match(/(Cr)(?<name1>y)(st)(?<name2>al)/).not_nil! match.captures # => ["Cr", "st"] # When this regex has an optional group, result array may contain # a `nil` if this group is not matched. match = "Crystal".match(/(Cr)(stal)?/).not_nil! match.captures # => ["Cr", nil]
Returns the position of the next character after the match.
When n is 0
or not given, uses the match of the entire Regex
. Otherwise, uses the match of the nth capture group.
"Crystal".match(/r/).not_nil!.end(0) # => 2 "Crystal".match(/r(ys)/).not_nil!.end(1) # => 4 "クリスタル".match(/リ(ス)/).not_nil!.end(0) # => 3
Returns the number of capture groups, including named capture groups.
"Crystal".match(/[p-s]/).not_nil!.group_size # => 0 "Crystal".match(/r(ys)/).not_nil!.group_size # => 1 "Crystal".match(/r(ys)(?<ok>ta)/).not_nil!.group_size # => 2
Appends this struct's name and instance variables names and values to the given IO.
struct Point def initialize(@x : Int32, @y : Int32) end end p1 = Point.new 1, 2 p1.to_s # "Point(@x=1, @y=2)" p1.inspect # "Point(@x=1, @y=2)"
Returns a hash of named capture groups.
match = "Crystal".match(/(Cr)(?<name1>y)(st)(?<name2>al)/).not_nil! match.named_captures # => {"name1" => "y", "name2" => "al"} # When this regex has an optional group, result hash may contain # a `nil` if this group is not matched. match = "Crystal".match(/(?<name1>Cr)(?<name2>stal)?/).not_nil! match.named_captures # => {"name1" => "Cr", "name2" => nil}
Returns the part of the original string after the match. If the match ends at the end of the string, returns the empty string.
"Crystal".match(/yst/).not_nil!.post_match # => "al"
Returns the part of the original string before the match. If the match starts at the start of the string, returns the empty string.
"Crystal".match(/yst/).not_nil!.pre_match # => "Cr"
Returns the original regular expression.
"Crystal".match(/[p-s]/).not_nil!.regex # => /[p-s]/
Returns the number of elements in this match object.
"Crystal".match(/[p-s]/).not_nil!.size # => 1 "Crystal".match(/r(ys)/).not_nil!.size # => 2 "Crystal".match(/r(ys)(?<ok>ta)/).not_nil!.size # => 3
Returns the original string.
"Crystal".match(/[p-s]/).not_nil!.string # => "Crystal"
Convert this match data into an array.
match = "Crystal".match(/(Cr)(?<name1>y)(st)(?<name2>al)/).not_nil! match.to_a # => ["Crystal", "Cr", "y", "st", "al"] # When this regex has an optional group, result array may contain # a `nil` if this group is not matched. match = "Crystal".match(/(Cr)(?<name1>stal)?/).not_nil! match.to_a # => ["Cr", "Cr", nil]
Convert this match data into a hash.
match = "Crystal".match(/(Cr)(?<name1>y)(st)(?<name2>al)/).not_nil! match.to_h # => {0 => "Crystal", 1 => "Cr", "name1" => "y", 3 => "st", "name2" => "al"} # When this regex has an optional group, result array may contain # a `nil` if this group is not matched. match = "Crystal".match(/(Cr)(?<name1>stal)?/).not_nil! match.to_h # => {0 => "Cr", 1 => "Cr", "name1" => nil}
Same as #inspect(io)
.
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Regex/MatchData.html