Skip to content
Related Articles

Related Articles

Ruby | MatchData Class
  • Last Updated : 30 Oct, 2019

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.

  1. [] : 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 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"]
    
  2. begin : This method returns the offset of the start of the nth element of the match array in the string.
    match.beign(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
    
  3. captures : This methods return the array of all the matching groups.
    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"]
    
  4. 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 program to illustrate 
    # use of end method
      
    # Using end method
    a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112.")
    a.end(0)
    a.end(2)

    
    

    Output:

    16
    13
    
  5. length : This method returns the number of elements present in the match array.
    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
    
  6. offset : This method return a two-element array that consist of starting and ending offset of the nth match.
    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]
    
  7. 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 program to illustrate 
    # use of post_match method
      
    # Using post_match method
    a = /(.)(.)(\d+)(\d)/.match("GeeksFORgeeks112: Ruby")
    a.post_match

    
    

    Output:

    ": Ruby"
  8. pre_match : This method returns the piece of the original string before the currentmatch. Identical to the special variable $`.
    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"
  9. select : This method return an array that consist of all the elements of match for which the block is true.
    match.select{|val|block}
  10. size : This method is similar to MatchData#length method.
    match.size
  11. string : This method return a frozen copy of the string passed in the match.
    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"
  12. to_a : This method returns the array of matches.
    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"]
  13. to_s : This method returns the whole matched string.
    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"
  14. 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 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"]
  15. == : 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

My Personal Notes arrow_drop_up
Recommended Articles
Page :