Section 3.2. Compiling Regular Expressions

3.1. Regular Expression Syntax

The typical regular expression is delimited by a pair of slashes; the %r form can also be used. Table 3.1, "Basic Regular Expressions," gives some simple examples:

Table 3.1. Basic Regular Expressions




Match the single word Ruby


Match Ruby or ruby


Match an abc at beginning of line


Match an xyz at end of line


Match any sequence of (zero or more) digits

It is also possible to place a modifier, consisting of a single letter, immediately after a regex. Table 3.2 shows the most common modifiers:

Table 3.2. Regular Expression Modifiers




Ignore case in regex


Perform expression substitution only once


Multiline mode (dot matches newline)


Extended regex (allow whitespace, comments)

Others will be covered in Chapter 4.

To complete our introduction to regular expressions, Table 3.3 lists the most common symbols and notations available:

Table 3.3. Common Notations Used in Regular Expressions




Beginning of a line or string


End of a line or string


Any character except newline (unless multiline)


Word character (digit, letter, or underscore)


Non-word character


Whitespace character (space, tab, newline, and so on)


Non-whitespace character


Digit (same as [0-9])




Beginning of a string


End of a string or before newline at the end


End of a string


Word boundary (outside [ ] only)


Non-word boundary


Backspace (inside [ ] only)


Any single character of set


0 or more of previous subexpression


0 or more of previous subexpression (non-greedy)


1 or more of previous subexpression


1 or more of previous subexpression (non-greedy)


m to n instances of previous subexpression


m to n instances of previous subexpression (non-greedy)


0 or 1 of previous regular expression



(?= )

Positive lookahead

(?! )

Negative lookahead


Grouping of subexpressions

(?> )

Embedded subexpression

(?: )

Non-capturing group


Turn options on/off henceforth


Turn options on/off for this expression

(?# )


An understanding of regex handling greatly benefits the modern programmer. A complete discussion of this topic is far beyond the scope of this book, but if you're interested see the definitive work Mastering Regular Expressions by Jeffrey Friedl.

For additions and extensions to the material in this section, refer to section 3.13, "Ruby and Oniguruma."

The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: