Skip to content

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

instance method

String#match?

match?(pattern, offset = 0) → true or false

Returns true or false based on whether a match is found for self and pattern.

Note: does not update Special global variables at Regexp.

Computes regexp by converting pattern (if not already a Regexp).

Example
regexp = Regexp.new(pattern)

Returns true if self+.match(regexp) returns a MatchData object, false otherwise:

Example
'foo'.match?(/o/) # => true
'foo'.match?('o') # => true
'foo'.match?(/x/) # => false

If Integer argument offset is given, the search begins at index offset:

Example
'foo'.match?('f', 1) # => false
'foo'.match?('o', 1) # => true
See also #match #next #lstrip! #next! also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0