Hour 16. Regular Expressions
What You’ll Learn in This Hour:
What regular expressions are
Defining regular expression patterns
How to use regular expressions in your scripts
One of the most common functions used in Python scripts is manipulation of string data. One of the
things Python is known for is its ability to easily search and modify strings. One of the features in
Python that provides support for string parsing is regular expressions. In this hour, you’ll see what
regular expressions are, how to use them in Python, and how to leverage them in your own Python
scripts.
What Are Regular Expressions?
Many people have a hard time understanding what regular expressions are. The first step to
understanding them is defining exactly what they are and what they can do for you. The following
sections explain what a regular expression is and describe how Python uses regular expressions to
help with your string manipulations.
Definition of Regular Expressions
A regular expression is a pattern you create to filter text. A program or script matches the regular
expression pattern you create against data as the data flows through the program. If the data matches
the pattern, it’s accepted for processing. If the data doesn’t match the pattern, it’s rejected. Figure
16.1 shows how it works.
FIGURE 16.1 Matching data against a regular expression.
While are probably familiar with normal text searching, regular expressions provides a lot more than
that. The regular expression pattern makes use of wildcard characters to represent one or more
characters in the data stream. You can use a number of special characters in a regular expression to
define a specific pattern for filtering data. This means you have a lot of flexibility in how you define
your string patterns.
Types of Regular Expressions