Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Developer Reading List

, April 02, 2013 New books on Java, Erlang, Unit Testing, Windows and more.
  • E-mail
  • Print

Regular Expressions Cookbook, 2nd Edition

by Jan Goyaverts and Steven Levithan

Regular expressions are probably the one aspect of programming that drives nearly every developer crazy at one point or another. Even if you love regular expressions, there is little defense to early Netscape engineer Jamie Zawinski's famous witticism: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems." There are essentially two solutions to the problem of using regular expressions (regex). The first is to study and practice them until you've mastered the patterns and can string them together effectively. This approach has several limitations: regex syntax is not portable across languages and, unless you use a lot of regular expressions, you're liable to slowly forget the all but the most primitive expressions. The second option is to do a quick Web search when a regex is needed. This is precisely what the Web is useful for— leveraging the experience of others who've had a similar need. The drawback is that you must find a regex formulation in your language that you know to be correct. There are lots of problems here: If it's not correct or if it's not exactly what you want, then you'll need to fix it, which brings you back to your original problem.

This book solves the second problem. It's a recipe book that's filled with dozens of examples for common situations. For example, validating the format of North American phone numbers. Each recipe shows the solution in seven languages: .NET (all .NET languages use the same regex engine), Java, JavaScript, Perl, PHP, Python, and Ruby. Each recipe also includes a careful explanation of how the syntax implements the right search. This allows you to modify the solutions easily.

The book also presents meta-level solutions; for example, iterating over all the matches found by a regular expression. (The JavaScript entry for this problem is well over a page long with discussions of browser compatibility issues.) This book shows all the careful attention to detail and thoroughness of preparation that I expect in a definitive reference volume. Recommended as an integral part of every developer's bookshelf.

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.