Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

String#match?

(::Regexp | ::string pattern, ?::int offset) -> bool
match?(pattern, offset = 0) → true or false

Returns whether a match is found for self and the given arguments; does not update Regexp Global Variables.

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

Example
regexp = Regexp.new(pattern)

The search for regexp in self begins at the given character offset. Returns true if a match is found, false otherwise:

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

Related: see Querying.

See also #match #next #lstrip! #next! also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0