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 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:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    11
    12
    
  3. captures : This methods return the array of all the matching groups.
    match.captures

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    16
    13
    
  5. length : This method returns the number of elements present in the match array.
    match.length

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:


    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    "GeeksFORgeeks112: Ruby"
  12. to_a : This method returns the array of matches.
    match.to_a

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    ["ks112", "k", "s", "11", "2"]
  13. to_s : This method returns the whole matched string.
    match.to_s

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.