O'Reilly logo

Learning Ruby by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Regular Expressions

Table A-6 lists regular expressions in Ruby.

Table A-6. Regular expressions in Ruby

Pattern

Description

/pattern/options

Pattern pattern in slashes, followed by optional options, one or more of: i for case-insensitive; o for substitute once; x for ignore whitespace, allow comments; m for match multiple lines and newlines as normal characters.

%r!pattern!

General delimited string for a regular expression, where ! can be an arbitrary character.

^

Matches beginning of line.

$

Matches end of line.

.

Matches any character.

\1...\9

Matches nth grouped subexpression.

\10

Matches nth grouped subexpression if already matched; otherwise, refers to octal representation of a character code.

\n, \r, \t, etc.

Matches character in backslash notation.

\w

Matches word character; same as [0-9A-Za-z_].

\W

Matches nonword character; same as [^0-9A-Za-z_].

\s

Matches whitespace character; same as [\t\n\r\f].

\S

Matches nonwhitespace character; same as [^\t\n\r\f].

\d

Matches digit; same as [0-9].

\D

Matches nondigit; same as [^0-9].

\A

Matches beginning of a string.

\Z

Matches end of a string, or before newline at the end.

\z

Matches end of a string.

\b

Matches word boundary outside [] or backspace (0x08) inside [].

\B

Matches nonword boundary.

\G

Matches point where last match finished.

[..]

Matches any single character in brackets, such as [ch].

[^..]

Matches any single character not in brackets.

*

Matches zero or more of previous regular ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required