Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Regexp#match?

rxp.match?(str) → true or false
rxp.match?(str, pos=0) → true or false

Returns true or false to indicate whether the regexp is matched or not without updating $~ and other related variables. If the second parameter is present, it specifies the position in the string to begin the search.

Example
/R.../.match?("Ruby")    #=> true
/R.../.match?("Ruby", 1) #=> false
/P.../.match?("Ruby")    #=> false
$&                       #=> nil
See also #match #named_captures #inspect #names also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0