- Explore more great deals on thousands of titles in our Deals in Books store.
To get the free app, enter your mobile phone number.
Regular Expressions Cookbook 1st Edition
Use the Amazon App to scan ISBNs and compare prices.
All Books, All the Time
Read author interviews, book reviews, editors picks, and more at the Amazon Book Review. Read it now
Customers who bought this item also bought
What other items do customers buy after viewing this item?
Special offers and product promotions
Whether you're a novice or an experienced user, Regular Expressions Cookbook will help deepen your understanding of the tool. You'll learn powerful new tricks, avoid language-specific gotchas, and save valuable time with this huge library of proven solutions to difficult, real-world problems.
Searching and Replacing with Regular Expressions
Search-and-replace is a common job for regular expressions. A search-and-replace function takes a subject string, a regular expression, and a replacement string as input. The output is the subject string with all matches of the regular expression replaced with the replacement text. Although the replacement text is not a regular expression at all, you can use certain special syntax to build dynamic replacement texts. All flavors let you reinsert the text matched by the regular expression or a capturing group into the replacement. Recipes 2.20 and 2.21 explain this. Some flavors also support inserting matched context into the replacement text, as Recipe 2.22 shows. In Chapter 3, Recipe 3.16 teaches you how to generate a different replacement text for each match in code.
Many Flavors of Replacement Text
Different ideas by different regular expression software developers have led to a wide range of regular expression flavors, each with different syntax and feature sets. The story for the replacement text is no different. In fact, there are even more replacement text flavors than regular expression flavors. Building a regular expression engine is difficult. Most programmers prefer to reuse an existing one, and bolting a search-and-replace function onto an existing regular expression engine is quite easy. The result is that there are many replacement text flavors for regular expression libraries that do not have built-in search-and-replace features.
Fortunately, all the regular expression flavors in this book have corresponding replacement text flavors, except PCRE. This gap in PCRE complicates life for programmers who use flavors based on it. The open source PCRE library does not include any functions to make replacements. Thus, all applications and programming languages that are based on PCRE need to provide their own search-and-replace function. Most programmers try to copy existing syntax, but never do so in exactly the same way.
This book covers the following replacement text flavors. Refer to “Many Flavors of Regular Expressions” on page 2 for more details on the regular expression flavors that correspond with the replacement text flavors:
Perl has built-in support for regular expression substitution via the s/regex/ replace/ operator. The Perl replacement text flavor corresponds with the Perl regular expression flavor. This book covers Perl 5.6 to Perl 5.10. The latter version adds support for named backreferences in the replacement text, as it adds named capture to the regular expression syntax.
In this book, the PHP replacement text flavor refers to the preg_replace function in PHP. This function uses the PCRE regular expression flavor and the PHP replacement text flavor.
Other programming languages that use PCRE do not use the same replacement text flavor as PHP. Depending on where the designers of your programming language got their inspiration, the replacement text syntax may be similar to PHP or any of the other replacement text flavors in this book. PHP also has an ereg_replace function. This function uses a different regular expression flavor (POSIX ERE), and a different replacement text flavor, too. PHP’s ereg functions are not discussed in this book.
The System.Text.RegularExpressions package provides various searchand- replace functions. The .NET replacement text flavor corresponds with the .NET regular expression flavor. All versions of .NET use the same replacement text flavor. The new regular expression features in .NET 2.0 do not affect the replacement text syntax.
The java.util.regex package has built-in search-and-replace functions. This book covers Java 4, 5, and 6. All use the same replacement text syntax.
Python’s re module provides a sub function to search-and-replace. The Python replacement text flavor corresponds with the Python regular expression flavor. This book covers Python 2.4 and 2.5. Python’s regex support has been stable for many years.
Ruby’s regular expression support is part of the Ruby language itself, including the search-and-replace function. This book covers Ruby 1.8 and 1.9. A default compilation of Ruby 1.8 uses the regular expression flavor provided directly by the Ruby source code, whereas a default compilation of Ruby 1.9 uses the Oniguruma regular expression library. Ruby 1.8 can be compiled to use Oniguruma, and Ruby 1.9 can be compiled to use the older Ruby regex flavor. In this book, we denote the native Ruby flavor as Ruby 1.8, and the Oniguruma flavor as Ruby 1.9. The replacement text syntax for Ruby 1.8 and 1.9 is the same, except that Ruby 1.9 adds support for named backreferences in the replacement text. Named capture is a new feature in Ruby 1.9 regular expressions.
Detailed Solutions in Eight Programming Languages --This text refers to an out of print or unavailable edition of this title.
Top customer reviews
Based on the fact I have given FIVE stars to the book, you may guess I was happy to find the information I needed to solve my problem.The book provides wealth of information on the syntax of regular expressions and their different flavors. The book represents a very well balanced fusion of the regular expression basics and details, which makes it a very valuable aid to any software developer.
Mastering Regular Expressions, by Jeffrey Friedl. (Available in 8 languages!) which is technical, and complete. Then, there is this one. Jan is the maintainer of the regex web site, and author of RegEx Buddy, EditPad Pro, and Regex Magic (and others)
If you are, or want to be a RegEx guru, you need these two books, EditPad Pro and RegEx buddy. The two softwares are tied into each other, and with the book, and the built in help forums in the software, you'll get your RegEx written and working effectively. If you ever have to work, and don't have access to the tools mentioned here, you will feel naked.
The range of examples is broad - in a good sense. Most of them are practical, real-world applications for most programmers, so understanding what they're trying to accomplish really helped in understanding the "how" of the implementation.
Note: If you are a Windows regex user, the Regexbuddy program is highly recommended - it has built-in libraries containing nearly all of the regexes provided in this "Cookbook" as well as built-in regex related code snippets for all of the programming languages as well. Its an excellent piece of software and an indispensible aid to the process of learning regular expressions. (It even has a built-in private forum where you can ask questions of the author directly!) And be sure to check out Jan's resource: [...] for free online regex tutorials and reference. Bottom line: Jan knows regular expressions and is very adept at explaining them!