Ruby | MatchData Class
In Ruby, all the pattern matching is done with the help of special variable $~. All the pattern matches will set the $~ to a MatchData that contains the information about the match. The MatchData objects are returned by the Regexp#match and Regexp.last_match methods. The MatchData objects enclosed all the result of the pattern match, results that are normally accessed by the special variables, i.e $&, $’, $`, $1, $2 etc.
Instance Method
Here match is the object of the MatchData Class.
- [] : It is known as Match Reference. In this MatchData is behaves like an array and may be accessed by using normal array indexing technique. In this match[0] is equivalent to special variable $& and it returns the entire matched string. match[1], match[2], match[3] and so on, return the match back reference value.
match[i] match[start, length] match[range]
Example:
Ruby
# Ruby program to illustrate# use of []# Using [] operatora = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks12.")a[0]a[1, 4]a[1..2]a[-2, 1] |
Output:
"ks12" ["k", "s", "1", "2"] ["k", "s"] ["1"]
- begin : This method returns the offset of the start of the nth element of the match array in the string.
match.begin(n)
Example:
Ruby
# Ruby program to illustrate# use of begin method# Using begin methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")a.begin(1)a.begin(2) |
Output:
11 12
- captures : This methods return the array of all the matching groups.
match.captures
Example:
Ruby
# Ruby program to illustrate# use of captures method# Using captures methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")a.captures |
Output:
["k", "s", "11", "2"]
- end : This method returns the offset into the character that immediately following the end of the nth element of thematch array in the string.
match.end(n)
Example:
Ruby
# Ruby program to illustrate# use of end method# Using end methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")a.end(0)a.end(2) |
Output:
16 13
- length : This method returns the number of elements present in the match array.
match.length
Example:
Ruby
# Ruby program to illustrate# use of length method# Using length methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")a.length# using size methoda.size |
Output:
5 5
- offset : This method return a two-element array that consist of starting and ending offset of the nth match.
match.offset(n)
Example:
Ruby
# Ruby program to illustrate# use of offset method# Using offset methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")a.offset(2)a.offset(1) |
Output:
[12, 13] [11, 12]
- post_match : This method returns the piece of the original string after the current match. Identical to the special variable $’.
match.post_match
Example:
Ruby
# Ruby program to illustrate# use of post_match method# Using post_match methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")a.post_match |
Output:
": Ruby"
- pre_match : This method returns the piece of the original string before the currentmatch. Identical to the special variable $`.
match.pre_match
Example:
Ruby
# Ruby program to illustrate# use of pre_match method# Using pre_match methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")a.pre_match |
Output:
"GeeksFORgee"
- select : This method return an array that consist of all the elements of match for which the block is true.
match.select{|val|block}- size : This method is similar to MatchData#length method.
match.size
- string : This method return a frozen copy of the string passed in the match.
match.string
Example:
Ruby
# Ruby program to illustrate# use of string method# Using string methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")a.string |
Output:
"GeeksFORgeeks112: Ruby"
- to_a : This method returns the array of matches.
match.to_a
Example:
Ruby
# Ruby program to illustrate# use of to_a method# Using to_a methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")a.to_a |
Output:
["ks112", "k", "s", "11", "2"]
- to_s : This method returns the whole matched string.
match.to_s
Example:
Ruby
# Ruby program to illustrate# use of to_s method# Using to_s methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")a.to_s |
Output:
"ks112"
- values_at : In this method index is used to access the matching values and return an array of corresponding match.
match.valu_at([index]*)
Example:
Ruby
# Ruby program to illustrate# use of values_at method# Using values_at methoda = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")a.values_at(2, 0) |
Output:
["s", "ks112"]
- == : It is known as Equality. It is used to check if both MatchData, i.e match1 and match2 are equal or not. If they are equal, then return true, otherwise return false.
match1==match2
Reference: https://ruby-doc.org/core-2.2.0/MatchData.html


