liveBook · Manning

Regular expression

As explained in The Quick Python Book, Fourth Edition

A regular expression is a formal pattern notation for matching and processing text strings.

A regular expression, or regex, is a pattern for recognizing text and often extracting data from it. Python’s `re` module provides pattern searches when looking for one fixed substring is too limiting—for example, when the text must consist of digits or may have several valid spellings. section 16.1 section 6.4.4

Pass a pattern string to `re.compile()` to obtain a compiled regex, then call its `search()` method on text. A successful search returns a match object; an unsuccessful one returns `None`. Compiling is optional, but can improve speed when processing large amounts of text. section 16.1

import re
regexp = re.compile("hello")
count = 0
file = open("textfile", 'r')
for line in file.readlines():
   if regexp.search(line):
       count = count + 1
file.close()
print(count)

This file-processing example searches each line for `hello`. It counts a line once even if `hello` occurs more than once on that line. section 16.1

Metacharacters let one pattern describe several possible strings:

  • `hello|Hello` matches either spelling; `(h|H)ello` limits the choice to the first letter. section 16.2
  • `[hH]ello` matches either first letter using a character class. A range such as `[a-z]` matches one character in that range. section 16.2
  • `\d` indicates a digit; `\D` can identify nondigits to remove before reformatting text. section 16.7 section 16.6.2

Prefer raw strings for patterns containing backslashes: prefix the Python string with `r` so Python does not process its escape sequences before `re` receives them. Otherwise, a pattern intended to match a literal backslash followed by `ten` can instead search for a tab followed by `en`. section 16.3 section 16.7

Normalize phone numbers

A phone-number formatter can use `re.sub(r'\D', '', phone_number)` to keep only digits, check the resulting length and number components, then use another substitution to add separators. Matching only expected separator patterns is another approach, but misses formats it did not anticipate. Test both numbers that should match and numbers that must fail; do not assume AI-generated patterns or tests are correct. section 16.6.2 section 16.6.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through compilation, matching, selected metacharacters, raw strings, and substitution; defers the full special-character reference to Python documentation.
Examples
Uses a `hello` line counter, capitalization variants, and a phone-number normalization exercise.
What it assumes
Assumes familiarity with Python strings and basic file-processing code; introduces regex matching with a literal pattern before adding metacharacters.
What you can do after
Apply it via Python’s `re` module to search and substitute text, while testing desired matches and nonmatches.
How it connects
Connects regex to Python strings, raw strings, text parsing, match objects, and substitution.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.