liveBook · Manning

Chapter 16: Regular expressions

What you will learn in chapter 16 of The Quick Python Book, Fourth Edition

The introduction explains why regular expressions deserve a place in a Python book despite being an advanced feature.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why regular expressions deserve a place in a Python book despite being an advanced feature.

Key terms: regular expressions, text parsing

16.1 What is a regular expression?

Introduces regular expressions through a program that searches text-file lines for “hello” and explains compiling and searching with Python’s re module.

Key terms: regular expression, regex, match, metacharacters, re, re.compile, compiled regular expression, search, substring, None

16.2 Regular expressions with special characters

This section shows how regex special characters can match alternatives, grouped choices, and single characters from a set or range.

Key terms: special character, vertical-bar special character, parentheses special characters, special characters, ranges of characters

16.3 Regular expressions and raw strings

This section explains why Python string escaping can change a regular expression before the regex engine sees it, and introduces two ways to preserve a literal backslash.

Key terms: normal Python strings, special sequences, re.compile, four backslashes, raw strings

16.3.1 Raw strings to the rescue

This section explains how raw strings preserve backslashes in Python string literals and why they are useful for regular expressions.

Key terms: raw string, special sequences, r, raw strings, regular expressions, regex special sequences, string special sequences

16.4 Extracting matched text from strings

The section builds a regex for names and variably formatted phone numbers, then shows how to extract matched fields and handle missing optional fields.

Key terms: [], +, metacharacter, \d, (), ?, {}, area code, named subpatterns, ?P<name>

16.5 Substituting text with regular expressions

This section explains how a regex’s sub method replaces matching text in a string.

Key terms: sub, invoking regex, matching substrings

16.5.1 Using a function with sub

Shows how to use a function with `sub` to compute a replacement string from each regular-expression match.

Key terms: sub, match object, group, named group, int_match_to_float

16.6 Phone number normalizer

This lab specifies how to normalize differently formatted US and Canadian phone numbers and offers optional validation rules.

Key terms: phone number normalizer, area code, exchange code, station code, country code, normalized phone number, ValueError, invalid phone number

16.6.1 Solving the problem with AI-generated code

Warns that AI-generated regular expressions need careful testing because they may match too much or too little.

Key terms: regular expression, positive matches, test cases

16.6.2 Solutions and discussion

16.7 Summary

Concepts explained in this chapter

← Chapter 15: Classes and object-oriented programming Chapter 17: Data types as objects →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.