Regular Expressions

Regular expressions are a minilanguage used to describe patterns of strings. A regular expression literal is a pattern between slashes or between arbitrary delimiters followed by %r:

/pattern/im          # option can be specified
%r!/usr/local!       # general delimited regular expression

Regular expressions have their own power and mystery; for more on this topic, see O’Reilly’s Mastering Regular Expressions by Jeffrey E.F. Friedl.

Regular-expression modifiers

Regular expression literals may include an optional modifier to control various aspects of matching. The modifier is specified after the second slash character, as shown previously and may be represented by one of these characters:




Substitutes only once


Ignores whitespace and allows comments in regular expressions


Matches multiple lines, recognizing newlines as normal characters

Regular-expression patterns

Except for control characters, (+ ? . * ^ $ ( ) [ ] { } | \), all characters match themselves. You can escape a control character by preceding it with a backslash.

Regular characters that express repetition (* + { }) can match very long strings, but when you follow such characters with control characters ?, you invoke a nongreedy match that finishes at the first successful match (i.e., +, *, etc.) followed by ? (i.e., +?, *?, etc.).


Matches beginning of line.


Matches end of line.


Matches any single character except newline. Using m option allows it to match newline as well.


Matches any ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.