O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Regular Expressions, First Edition

Book Description

Learn to use one of the most powerful text processing and manipulation tools available

Regular expression experts have long been armed with an incredibly powerful tool, one that can be used to perform all sorts of sophisticated text processing and manipulation in just about every language and on every platform. That’s the good news. The bad news is that for too long, regular expressions have been the exclusive property of only the most tech savvy. Until now.

Ben Forta's Learning Regular Expressions teaches you the regular expressions that you really need to know, starting with simple text matches and working up to more complex topics, including the use of backreferences, conditional evaluation, and look-ahead processing. You’ll learn what you can use, and you’ll learn it methodically, systematically, and simply.

Regular expressions are nowhere near as complex as they appear to be at first glance. All it takes is a clear understanding of the problem being solved and how to leverage regular expressions to solve them.

  • Read and understand regular expressions
  • Use literal text and metacharacters to build powerful search patterns
  • Take advantage of advanced regular expression features, including lookahead and backreferences
  • Perform powerful search-and-replace operations in all major professional editing tools
  • Add sophisticated form and text processing to web applications
  • Search for files using command-line tools like grep and egrep
  • Use regular expressions in programming languages like JavaScript, Java, PHP, Python, Microsoft .NET, and C#, as well as in DBMSs including MySQL and Oracle
  • Work with phone numbers, postal codes, social security numbers, IP addresses, URLs, email addresses, and credit card numbers

Contents at a Glance

1 Introducing Regular Expressions

2 Matching Single Characters

3 Matching Sets of Characters

4 Using Metacharacters

5 Repeating Matches

6 Position Matching

7 Using Subexpressions

8 Using Backreferences

9 Looking Ahead and Behind

10 Embedding Conditions

11 Regular Expression Solutions to Common Problems

Appendix A Regular Expressions in Popular Applications and Languages

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Contents at a Glance
  5. Table of Contents
  6. About This E-Book
  7. About the Author
  8. Accessing the Free Web Edition
  9. Introduction
    1. Who Is Learning Regular Expressions For?
  10. 1 Introducing Regular Expressions
    1. Understanding the Need
    2. How Regular Expressions Are Used
      1. RegEx Searches
      2. RegEx Replaces
    3. So What Exactly Is a Regular Expression?
    4. Using Regular Expressions
    5. Before You Get Started
    6. Summary
  11. 2 Matching Single Characters
    1. Matching Literal Text
      1. How Many Matches?
      2. Handling Case Sensitivity
    2. Matching Any Characters
    3. Matching Special Characters
    4. Summary
  12. 3 Matching Sets of Characters
    1. Matching One of Several Characters
    2. Using Character Set Ranges
    3. “Anything But” Matching
    4. Summary
  13. 4 Using Metacharacters
    1. Escaping Revisited
    2. Matching Whitespace Characters
    3. Matching Specific Character Types
      1. Matching Digits (and Nondigits)
      2. Matching Alphanumeric Characters (and Nonalphanumeric Characters)
      3. Matching Whitespace (and Nonwhitespace)
      4. Specifying Hexadecimal or Octal Values
    4. Using POSIX Character Classes
    5. Summary
  14. 5 Repeating Matches
    1. How Many Matches?
      1. Matching One or More Characters
      2. Matching Zero or More Characters
      3. Matching Zero or One Characters
    2. Using Intervals
      1. Exact Interval Matching
      2. Range Interval Matching
      3. “At Least” Interval Matching
    3. Preventing Over Matching
    4. Summary
  15. 6 Position Matching
    1. Using Boundaries
    2. Using Word Boundaries
    3. Defining String Boundaries
      1. Using Multiline Mode
    4. Summary
  16. 7 Using Subexpressions
    1. Understanding Subexpressions
    2. Grouping with Subexpressions
    3. Nesting Subexpressions
    4. Summary
  17. 8 Using Backreferences
    1. Understanding Backreferences
    2. Matching with Backreferences
    3. Performing Replace Operations
      1. Converting Case
    4. Summary
  18. 9 Looking Ahead and Behind
    1. Introducing Lookaround
    2. Looking Ahead
    3. Looking Behind
    4. Combining Lookahead and Lookbehind
    5. Negating Lookaround
    6. Summary
  19. 10 Embedding Conditions
    1. Why Embed Conditions?
    2. Using Conditions
      1. Backreference Conditions
      2. Lookaround Conditions
    3. Summary
  20. 11 Regular Expression Solutions to Common Problems
    1. North American Phone Numbers
    2. U.S. ZIP Codes
    3. Canadian Postal Codes
    4. United Kingdom Postcodes
    5. U.S. Social Security Numbers
    6. IP Addresses
    7. URLs
    8. Complete URLs
    9. Email Addresses
    10. HTML Comments
    11. JavaScript Comments
    12. Credit Card Numbers
    13. Summary
  21. Appendix A Regular Expressions in Popular Applications and Languages
    1. grep
    2. Java
    3. JavaScript
    4. Microsoft .NET
    5. Microsoft SQL Server T-SQL
    6. Microsoft Visual Studio .NET
    7. MySQL
    8. Oracle PL/SQL
    9. Perl
    10. PHP
    11. Python
  22. Index