Cover by David Flanagan, Yukihiro Matsumoto

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Regular Expressions

A regular expression (also known as a regexp or regex) describes a textual pattern. Ruby’s Regexp class[*] implements regular expressions, and both Regexp and String define pattern matching methods and operators. Like most languages that support regular expressions, Ruby’s Regexp syntax follows closely (but not precisely) the syntax of Perl 5.

Regexp Literals

Regular expression literals are delimited by forward slash characters:

/Ruby?/  # Matches the text "Rub" followed by an optional "y"

The closing slash character isn’t a true delimiter because a regular expression literal may be followed by one or more optional flag characters that specify additional information about the how pattern matching is to be done. For example:

/ruby?/i  # Case-insensitive: matches "ruby" or "RUB", etc.
/./mu     # Matches Unicode characters in Multiline mode

The allowed modifier characters are shown in Table 9-1.

Table 9-1. Regular expression modifier characters

ModifierDescription
i Ignore case when matching text.
m The pattern is to be matched against multiline text, so treat newline as an ordinary character: allow . to match newlines.
x Extended syntax: allow whitespace and comments in regexp.
o

Perform #{} interpolations only once, the first time the regexp literal is evaluated.

u,e,s,n

Interpret the regexp as Unicode (UTF-8), EUC, SJIS, or ASCII. If none of these modifiers is specified, the regular expression is assumed to use the source encoding.

Like string literals delimited with %Q, Ruby ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required