struct 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.

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.

Included Modules

Defined in:

helper_classes/Regex.cr

Constructors

Constructor Detail

def self.new(regex : Regex, code : Pointer(LibPCRE2::Code), string : String, pos : Int32, ovector : Pointer(LibC::SizeT), group_size : Int32) #

[View source]