class Regex::MatchData
Overview
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.
Regex#match
and String#match
can return nil
, to represent an
unsuccessful match, but there are overloads of both methods that accept a
block. These overloads are convenient to access the Regex::MatchData
of a
successful match, since the block argument can't be nil
.
"Crystal".match(/[p-s]/).size # => undefined method 'size' for Nil
"Crystal".match(/[p-s]/) do |md|
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.
Defined in:
regex/match_data.crInstance Method Summary
-
#[](group_name : String)
Returns the match of the capture group named by
group_name
, or raises anArgumentError
if there is no such named capture group. -
#[](n)
Returns the match of the
n
th capture group, or raises anIndexError
if there is non
th capture group. -
#[]?(group_name : String)
Returns the match of the capture group named by
group_name
, ornil
if there is no such named capture group. -
#[]?(n)
Returns the match of the
n
th capture group, ornil
if there isn't ann
th capture group. -
#begin(n = 0)
Return the position of the first character of the
n
th match. -
#byte_begin(n = 0)
Return the position of the first byte of the
n
th match. -
#byte_end(n = 0)
Return the position of the next byte after the match.
-
#end(n = 0)
Return the position of the next character after the match.
- #inspect(io : IO)
-
#post_match
Returns the part of the original string after the match.
-
#pre_match
Returns the part of the original string before the match.
-
#regex : Regex
Returns the original regular expression.
-
#size : Int32
Returns the number of capture groups, including named capture groups.
-
#string : String
Returns the original string.
- #to_s(io : IO)
Instance methods inherited from class Reference
==(other)==(other : self) ==, hash hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, same?(other : Reference)
same?(other : Nil) same?, to_s(io : IO) : Nil to_s
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO) inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
from_json(string_or_io) : self
from_json,
from_yaml(string : String) : self
from_yaml,
hash
hash,
inspect(io)
inspect,
name : String
name,
to_s(io)
to_s,
|(other : U.class)
|
Instance Method Detail
Returns the match of the capture group named by group_name
, or
raises an ArgumentError
if there is no such named capture group.
"Crystal".match(/r(?<ok>ys)/) { |md| md["ok"] } # => "ys"
"Crystal".match(/r(?<ok>ys)/) { |md| md["ng"] } # => raises ArgumentError
Returns the match of the n
th capture group, or raises an IndexError
if there is no n
th capture group.
"Crystal".match(/r(ys)/) { |md| md[1] } # => "ys"
"Crystal".match(/r(ys)/) { |md| md[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)/) { |md| md["ok"]? } # => "ys"
"Crystal".match(/r(?<ok>ys)/) { |md| md["ng"]? } # => nil
Returns the match of the n
th capture group, or nil
if there isn't
an n
th capture group.
When n
is 0
, returns the match for the entire Regex
.
"Crystal".match(/r(ys)/) { |md| md[0]? } # => "rys"
"Crystal".match(/r(ys)/) { |md| md[1]? } # => "ys"
"Crystal".match(/r(ys)/) { |md| md[2]? } # => nil
Return the position of the first character of the n
th match.
When n
is 0
or not given, uses the match of the entire Regex
.
Otherwise, uses the match of the n
th capture group.
"Crystal".match(/r/) { |md| md.begin(0) } # => 1
"Crystal".match(/r(ys)/) { |md| md.begin(1) } # => 2
"クリスタル".match(/リ(ス)/) { |md| md.begin(0) } # => 1
Return the position of the first byte of the n
th match.
When n
is 0
or not given, uses the match of the entire Regex
.
Otherwise, uses the match of the n
th capture group.
"Crystal".match(/r/) { |md| md.byte_begin(0) } # => 1
"Crystal".match(/r(ys)/) { |md| md.byte_begin(1) } # => 4
"クリスタル".match(/リ(ス)/) { |md| md.byte_begin(0) } # => 3
Return 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 n
th capture group.
"Crystal".match(/r/) { |md| md.byte_end(0) } # => 2
"Crystal".match(/r(ys)/) { |md| md.byte_end(1) } # => 4
"クリスタル".match(/リ(ス)/) { |md| md.byte_end(0) } # => 9
Return 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 n
th capture group.
"Crystal".match(/r/) { |md| md.end(0) } # => 2
"Crystal".match(/r(ys)/) { |md| md.end(1) } # => 4
"クリスタル".match(/リ(ス)/) { |md| md.end(0) } # => 3
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/) { |md| md.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/) { |md| md.pre_match } # => "Cr"
Returns the original regular expression.
"Crystal".match(/[p-s]/) { |md| md.regex } # => /[p-s]/
Returns the number of capture groups, including named capture groups.
"Crystal".match(/[p-s]/) { |md| md.size } # => 0
"Crystal".match(/r(ys)/) { |md| md.size } # => 1
"Crystal".match(/r(ys)(?<ok>ta)/) { |md| md.size } # => 2
Returns the original string.
"Crystal".match(/[p-s]/) { |md| md.string } # => "Crystal"