Name

MatchData — Class for holding regular expression pattern match data

Synopsis

MatchData objects can be retrieved from the variable $~ or as return values from Regexp.match.

Example

if m = pat.match(str)   # MatchData object on success
  print "matched: ", m[0], "\n"
  print "pre: ", m.pre_match, "\n"
  print "post: ", m.post_match, "\n"
end

Instance Methods

m[n]

Returns the match corresponding to the nth group of the regular expression. If n is 0, the entire matched string is returned.

m.begin(n)

Returns the offset of the start of the match corresponding to the nth group of the regular expression. If n is 0, the offset of the start of the entire matched string is returned.

m.end(n)

Returns the offset of the end of the match corresponding to the nth group of the regular expression. If n is 0, the offset of the end of the entire matched string is returned.

m.length

See m.size

m.offset(n)

Returns a two-element array containing the beginning and ending offsets of the string corresponding to the nth group of the regular expression.

m.post_match

Returns the part of the original string following the matched string.

m.pre_match

Returns the part of the original string preceding the matched string.

m.size
m.length

Returns the number of groups in the regular expression +1.

m.string

Returns the original string used for the match.

m.to_a

Returns an array of the matches (i.e.,[$&, $1, $2...]).

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.