Name

Regexp — Regular expression class

Synopsis

Regex is object representation of regular expression. Regular expression is a mini-language to describe patterns of strings. For its syntax, see “Regular-expression patterns,” which is under Section 2.4.6 in Chapter 2.

Class Methods

Regexp::new(str[, option[, code]])
Regexp::compile(str[, option[, code]])

Creates a Regexp object. option may be a logical OR of Regexp::IGNORECASE, Regexp::EXTENDED, and Regexp::MULTILINE. code may be a string specifying a multibyte character set code.

Regexp::escape(str)
Regexp::quote(str)

Returns a copy of str with all regular expression meta characters escaped.

Instance Methods

~ r

Performs a regular expression match against $_. Equivalent to r =~ $_. This method is obsolete.

r === str

Synonym for r =~ str used in case statements.

r =~ str

Performs a regular expression match, returning the offset of the start of the match, or nil if the match failed.

r.casefold?

Returns true if the Regexp object is case-insensitive.

r.match(str)

Performs a regular expression match, returning the resulting match information as a MatchData object, or nil if the match failed.

if m = /fo*b.r+/.match(str)
  puts m[0]          # print matched string
end
r.source

Returns the original regular expression pattern string.

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.