- Paperback: 510 pages
- Publisher: O'Reilly Media; 1 edition (June 1, 2009)
- Language: English
- ISBN-10: 0596520689
- ISBN-13: 978-0596520687
- Product Dimensions: 7 x 1.2 x 9.2 inches
- Shipping Weight: 1.8 pounds
- Average Customer Review: 55 customer reviews
- Amazon Best Sellers Rank: #1,345,566 in Books (See Top 100 in Books)
Enter your mobile number or email address below and we'll send you a link to download the free Kindle App. Then you can start reading Kindle books on your smartphone, tablet, or computer - no Kindle device required.
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.
Fulfillment by Amazon (FBA) is a service we offer sellers that lets them store their products in Amazon's fulfillment centers, and we directly pack, ship, and provide customer service for these products. Something we hope you'll especially enjoy: FBA items qualify for FREE Shipping and Amazon Prime.
If you're a seller, Fulfillment by Amazon can help you increase your sales. We invite you to learn more about Fulfillment by Amazon .
There is a newer edition of this item:
See the Best Books of 2018 So Far
Looking for something great to read? Browse our editors' picks for the best books of the year so far in fiction, nonfiction, mysteries, children's books, and much more.
Customers who viewed this item also viewed
Customers who bought this item also bought
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
Top customer reviews
There was a problem filtering reviews right now. Please try again later.
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.
The last great book on Regular Expressions was "Mastering Regular Expressions" by Jeffrey E. F. Friedl, also published by O'Reilly. This book does not replace "Mastering Regular Expressions", but complements it. Between the two volumes, you'll know everything of importance worth knowing about Regular Expressions and their use.
Regular Expressions are used to find specific patterns of text. For anyone working extensively with text of any kind, Regular Expressions are as necessary as water and air to sustaining human life. Most people never get behind the primitive search functions of their word processor or spreadsheet program. Too bad: they're missing a lot.
The ugly part of what they're missing is learning how to use Regular Expressions.
Conceptually, Regular Expressions are difficult for many people (like me) to grasp and even more difficult to learn. A big part of that is the staggering power of Regular Expressions ("regexp" or "regexes"). Want to a single search for specific words that are misspelled? Regex. How about sentences beginning or ending with specific words? Use a regex.
In their cookbook, the authors demonstrate more than a hundred examples. Better yet, they do it in seven common regex flavors. The authors claim "Regular Expressions Cookbook" is all you know to learn how to use Regular Expressions. They do start with the basics, but I question whether this book is all most will need. I think consulting one of the many fine Regular Expression tutorials on the web might be a helpful first step for the utter novice.
The cookbook itself is absolutely marvelous.
There are more than one hundred recipes, beginning with matching literal text; advancing through matching previously matched text again; retrieving a list of all matches; validating formats of things like email addresses, international phone numbers, even European VAT numbers; finding words not preceded or followed by a specific word; and much more.
This is, I short, a book for the true geek to curl up with and read. You may not need the information now, but you will need it someday and just browsing is an effective way to pick it up. Likewise, if you're looking for an immediate solution to a problem right now, just check the Table Of Contents. Odds are you'll find what you're looking for or something real close. Sadly, however, the index isn't very good.
In short, this is the newest benchmark reference for Regular Expressions. With this and "Mastering Regular Expressions", you are going to be a Master of the Universe and do things with text that will leave ordinary mortals awestruck.
Most recent customer reviews
It is of interest to see how differently "regular" expressions are constructed and used in...Read more
Mastering Regular Expressions, by Jeffrey Friedl. (Available in 8 languages!) which is technical, and complete.Read more