Open In App

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.



match[i]
match[start, length]
match[range]

Example:




# Ruby program to illustrate
# use of []
 
# Using [] operator
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks12.")
a[0]
a[1, 4]
a[1..2]
a[-2, 1

Output: 



"ks12"
["k", "s", "1", "2"]
["k", "s"]
["1"]
match.begin(n)

Example:




# Ruby program to illustrate
# use of begin method
 
# Using begin method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.begin(1)
a.begin(2)

Output: 

11
12
match.captures

Example:




# Ruby program to illustrate
# use of captures method
 
# Using captures method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.captures

Output: 

["k", "s", "11", "2"]
match.end(n)

Example:




# Ruby program to illustrate
# use of end method
 
# Using end method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.end(0)
a.end(2)

Output: 

16
13
match.length

Example:




# Ruby program to illustrate
# use of length method
 
# Using length method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.length
 
# using size method
a.size

Output: 

5
5
match.offset(n)

Example:




# Ruby program to illustrate
# use of offset method
 
# Using offset method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
a.offset(2)
a.offset(1)

Output: 

[12, 13]
[11, 12]
match.post_match

Example: 




# Ruby program to illustrate
# use of post_match method
 
# Using post_match method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.post_match

Output: 

": Ruby"
match.pre_match

Example:




# Ruby program to illustrate
# use of pre_match method
 
# Using pre_match method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.pre_match

Output: 

"GeeksFORgee"
match.select{|val|block}
match.size
match.string

Example:




# Ruby program to illustrate
# use of string method
 
# Using string method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.string

Output: 

"GeeksFORgeeks112: Ruby"
match.to_a

Example:




# Ruby program to illustrate
# use of to_a method
 
# Using to_a method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.to_a

Output:

["ks112", "k", "s", "11", "2"]
match.to_s

Example: 




# Ruby program to illustrate
# use of to_s method
 
# Using to_s method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.to_s

Output: 

"ks112"
match.valu_at([index]*)

Example:




# Ruby program to illustrate
# use of values_at method
 
# Using values_at method
a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
a.values_at(2, 0)

Output:

["s", "ks112"]
match1==match2

Reference: https://ruby-doc.org/core-2.2.0/MatchData.html


Article Tags :