Synopses & Reviews
Take the guesswork out of using regular expressions. With more than 140 practical recipes, this cookbook provides everything you need to solve a wide range of real-world problems. Novices will learn basic skills and tools, and programmers and experienced users will find a wealth of detail. Each recipe provides samples you can use right away.
This revised edition covers the regular expression flavors used by C#, Java, JavaScript, Perl, PHP, Python, Ruby, and VB.NET. Youll learn powerful new tricks, avoid flavor-specific gotchas, and save valuable time with this huge library of practical solutions.
- Learn regular expressions basics through a detailed tutorial
- Use code listings to implement regular expressions with your language of choice
- Understand how regular expressions differ from language to language
- Handle common user input with recipes for validation and formatting
- Find and manipulate words, special characters, and lines of text
- Detect integers, floating-point numbers, and other numerical formats
- Parse source code and process log files
- Use regular expressions in URLs, paths, and IP addresses
- Manipulate HTML, XML, and data exchange formats
- Discover little-known regular expression tricks and techniques
Synopsis
Take the guesswork out using regular expressions to search and manipulate text. With this updated cookbook, you have access to hundreds of proven recipes for todays most popular programming languages—including C#, Java, JavaScript (including XRegExp), Perl, PHP, Python, Ruby, and VB.NET.
As useful as regular expressions can be for programmers, their power doesn't come worry-free. This guide will deepen your understanding, no matter how much experience you have with this reliable tool. Youll learn powerful new tricks, steer clear of flavor-specific gotchas, and save valuable time with this huge library of solutions to difficult, real-world problems.
- Understand the basics of regular expressions through a concise tutorial
- Use regular expressions effectively in several programming and scripting languages
- Learn how to validate and format input
- Manage words, lines, special characters, and numerical values
- Find solutions for using regular expressions in URLs, paths, markup, and data exchange
- Discover the nuances of more advanced regex features
- Experience how regular expressions' APIs, syntax, and behavior differ from language to language
- Write better regular expressions for custom needs
About the Author
Jan Goyvaerts runs Just Great Software, where he designs and develops some of the most popular regular expression software. His products include RegexBuddy, the world's only regular expression editor that emulates the peculiarities of 15 regular expression flavors, and PowerGREP, the most feature-rich grep tool for Microsoft Windows.
Steve Levithan works at Facebook as a JavaScript engineer. He has enjoyed programming for nearly 15 years, working in Tokyo, Washington D.C., Baghdad, and Silicon Valley. Steven is a leading JavaScript regular expression expert, and has created a variety of open source regular expression tools including RegexPal and the XRegExp library.
Table of Contents
Preface; Caught in the Snarls of Different Versions; Intended Audience; Technology Covered; Organization of This Book; Conventions Used in This Book; Using Code Examples; Safari® Books Online; How to Contact Us; Acknowledgments; Chapter 1: Introduction to Regular Expressions; 1.1 Regular Expressions Defined; 1.2 Search and Replace with Regular Expressions; 1.3 Tools for Working with Regular Expressions; Chapter 2: Basic Regular Expression Skills; 2.1 Match Literal Text; 2.2 Match Nonprintable Characters; 2.3 Match One of Many Characters; 2.4 Match Any Character; 2.5 Match Something at the Start and/or the End of a Line; 2.6 Match Whole Words; 2.7 Unicode Code Points, Categories, Blocks, and Scripts; 2.8 Match One of Several Alternatives; 2.9 Group and Capture Parts of the Match; 2.10 Match Previously Matched Text Again; 2.11 Capture and Name Parts of the Match; 2.12 Repeat Part of the Regex a Certain Number of Times; 2.13 Choose Minimal or Maximal Repetition; 2.14 Eliminate Needless Backtracking; 2.15 Prevent Runaway Repetition; 2.16 Test for a Match Without Adding It to the Overall Match; 2.17 Match One of Two Alternatives Based on a Condition; 2.18 Add Comments to a Regular Expression; 2.19 Insert Literal Text into the Replacement Text; 2.20 Insert the Regex Match into the Replacement Text; 2.21 Insert Part of the Regex Match into the Replacement Text; 2.22 Insert Match Context into the Replacement Text; Chapter 3: Programming with Regular Expressions; 3.1 Programming Languages and Regex Flavors; 3.2 Literal Regular Expressions in Source Code; 3.3 Import the Regular Expression Library; 3.4 Create Regular Expression Objects; 3.5 Set Regular Expression Options; 3.6 Test If a Match Can Be Found Within a Subject String; 3.7 Test Whether a Regex Matches the Subject String Entirely; 3.8 Retrieve the Matched Text; 3.9 Determine the Position and Length of the Match; 3.10 Retrieve Part of the Matched Text; 3.11 Retrieve a List of All Matches; 3.12 Iterate over All Matches; 3.13 Validate Matches in Procedural Code; 3.14 Find a Match Within Another Match; 3.15 Replace All Matches; 3.16 Replace Matches Reusing Parts of the Match; 3.17 Replace Matches with Replacements Generated in Code; 3.18 Replace All Matches Within the Matches of Another Regex; 3.19 Replace All Matches Between the Matches of Another Regex; 3.20 Split a String; 3.21 Split a String, Keeping the Regex Matches; 3.22 Search Line by Line; 3.23 Construct a Parser; Chapter 4: Validation and Formatting; 4.1 Validate Email Addresses; 4.2 Validate and Format North American Phone Numbers; 4.3 Validate International Phone Numbers; 4.4 Validate Traditional Date Formats; 4.5 Validate Traditional Date Formats, Excluding Invalid Dates; 4.6 Validate Traditional Time Formats; 4.7 Validate ISO 8601 Dates and Times; 4.8 Limit Input to Alphanumeric Characters; 4.9 Limit the Length of Text; 4.10 Limit the Number of Lines in Text; 4.11 Validate Affirmative Responses; 4.12 Validate Social Security Numbers; 4.13 Validate ISBNs; 4.14 Validate ZIP Codes; 4.15 Validate Canadian Postal Codes; 4.16 Validate U.K. Postcodes; 4.17 Find Addresses with Post Office Boxes; 4.18 Reformat Names From "FirstName LastName" to "LastName, FirstName"; 4.19 Validate Password Complexity; 4.20 Validate Credit Card Numbers; 4.21 European VAT Numbers; Chapter 5: Words, Lines, and Special Characters; 5.1 Find a Specific Word; 5.2 Find Any of Multiple Words; 5.3 Find Similar Words; 5.4 Find All Except a Specific Word; 5.5 Find Any Word Not Followed by a Specific Word; 5.6 Find Any Word Not Preceded by a Specific Word; 5.7 Find Words Near Each Other; 5.8 Find Repeated Words; 5.9 Remove Duplicate Lines; 5.10 Match Complete Lines That Contain a Word; 5.11 Match Complete Lines That Do Not Contain a Word; 5.12 Trim Leading and Trailing Whitespace; 5.13 Replace Repeated Whitespace with a Single Space; 5.14 Escape Regular Expression Metacharacters; Chapter 6: Numbers; 6.1 Integer Numbers; 6.2 Hexadecimal Numbers; 6.3 Binary Numbers; 6.4 Octal Numbers; 6.5 Decimal Numbers; 6.6 Strip Leading Zeros; 6.7 Numbers Within a Certain Range; 6.8 Hexadecimal Numbers Within a Certain Range; 6.9 Integer Numbers with Separators; 6.10 Floating-Point Numbers; 6.11 Numbers with Thousand Separators; 6.12 Add Thousand Separators to Numbers; 6.13 Roman Numerals; Chapter 7: Source Code and Log Files; 7.1 Keywords; 7.2 Identifiers; 7.3 Numeric Constants; 7.4 Operators; 7.5 Single-Line Comments; 7.6 Multiline Comments; 7.7 All Comments; 7.8 Strings; 7.9 Strings with Escapes; 7.10 Regex Literals; 7.11 Here Documents; 7.12 Common Log Format; 7.13 Combined Log Format; 7.14 Broken Links Reported in Web Logs; Chapter 8: URLs, Paths, and Internet Addresses; 8.1 Validating URLs; 8.2 Finding URLs Within Full Text; 8.3 Finding Quoted URLs in Full Text; 8.4 Finding URLs with Parentheses in Full Text; 8.5 Turn URLs into Links; 8.6 Validating URNs; 8.7 Validating Generic URLs; 8.8 Extracting the Scheme from a URL; 8.9 Extracting the User from a URL; 8.10 Extracting the Host from a URL; 8.11 Extracting the Port from a URL; 8.12 Extracting the Path from a URL; 8.13 Extracting the Query from a URL; 8.14 Extracting the Fragment from a URL; 8.15 Validating Domain Names; 8.16 Matching IPv4 Addresses; 8.17 Matching IPv6 Addresses; 8.18 Validate Windows Paths; 8.19 Split Windows Paths into Their Parts; 8.20 Extract the Drive Letter from a Windows Path; 8.21 Extract the Server and Share from a UNC Path; 8.22 Extract the Folder from a Windows Path; 8.23 Extract the Filename from a Windows Path; 8.24 Extract the File Extension from a Windows Path; 8.25 Strip Invalid Characters from Filenames; Chapter 9: Markup and Data Formats; 9.1 Processing Markup and Data Formats with Regular Expressions; 9.2 Find XML-Style Tags; 9.3 Replace Tags with
; 9.4 Remove All XML-Style Tags Except and ; 9.5 Match XML Names; 9.6 Convert Plain Text to HTML by Adding and
Tags; 9.7 Decode XML Entities; 9.8 Find a Specific Attribute in XML-Style Tags; 9.9 Add a cellspacing Attribute to
Tags That Do Not Already Include It; 9.10 Remove XML-Style Comments; 9.11 Find Words Within XML-Style Comments; 9.12 Change the Delimiter Used in CSV Files; 9.13 Extract CSV Fields from a Specific Column; 9.14 Match INI Section Headers; 9.15 Match INI Section Blocks; 9.16 Match INI Name-Value Pairs; Colophon;