liveBook · Manning

Regular expression

As explained in Python How-To

A regular expression pattern is a textual specification describing character sequences and structural conditions to be matched in text.

Need to find records in messy text or split on several kinds of separator? A regular expression (regex) describes the character sequences to match. It becomes useful when ordinary string methods would require several passes or cannot express the pattern clearly. In Python, the standard-library re module can search with a pattern directly or compile one for repeated use. section 2.3.3 section 2.4 section 2.4.1

Build a pattern from the shape of the data:

  • Use character classes such as \d for a digit and \w for a word character; [,_] matches either separator. A raw string, such as r"\d+", keeps Python’s string escaping from obscuring the regex. section 2.4.2 section 2.4.3 section 2.4
  • Use ^ and $ for string boundaries, and quantifiers such as ? (zero or one), * (zero or more), + (one or more), or {3} (exactly three). Quantifiers normally match as much as possible; adding ? makes them non-greedy. section 2.4.3
  • Use parentheses to group parts you want to extract. Prefer named groups when several fields would otherwise be hard to identify. section 2.5.1 section 2.5.4

Find task records among unrelated lines

A valid line has a three-digit ID, a comma and space, a one-word title, a semicolon and space, and a description. Test that shape before processing its fields: import re pattern = re.compile(r"(\d{3}), (\w+); (.+)") for line in ["101, Homework; Complete physics and math", "some random nonsense", "54, random; record"]: match = pattern.match(line) print(match.group() if match else "No match") Output: 101, Homework; Complete physics and math No match No match Here, match checks the beginning of each line. If the entire line must fit, consider an end anchor too. section 2.5.1 section 2.4.5 section 2.4.3

Table 2.3 Common regular expression methods (view table figure)

Method

Code example

Match/return value

search: Returns a Match if a match is found anywhere in the string.

re.search(r"\d+", "ab12xy")

'12'

re.search(r"\d+", "abxy")

None

match: Returns a Match only if a match is found at the string’s beginning.

re.match(r"\d+", "ab12xy")

None

re.match(r"\d+", "12abxy")

'12'

findall: Returns a list of strings that match the pattern. When the pattern has multiple groups, the item is a tuple.

re.findall(r"h[ie]\w", "hi hey hello")

['hey', 'hel']

re.findall(r"(h|H)(i|e)", "Hey hello")

[('H', 'e'), ('h', 'e')]

finditer: Returns an iterator3 that yields the Match objects.

re.finditer(r"(h|H)(i|e)", "hi Hey hello")

An iterator

split: Splits the string by the pattern.

re.split(r"\d+", 'a1b2c3d4e')

['a', 'b', 'c', 'd', 'e']

sub: Creates a string by replacing the matched with the replacement.

re.sub(r"\D", "-", '123,456_789')

'123-456-789'

Compare search and match for one result, findall and finditer for multiple results, split for separators, and sub for replacement. section 2.4.5

Start with the required data shape, then create a pattern, inspect its matches on representative text, and revise it for edge cases. Use simpler methods such as startswith or endswith when they express the requirement clearly; a regex that merely works is not necessarily the easiest solution to maintain. section 2.4.6 section 2.4.3

How Python How-To covers it

How deep it goes
Explains pattern syntax, matching methods, raw strings, and an iterative record-extraction workflow without descending into regex engine internals.
Examples
Tests anchors and quantifiers, splits comma-or-underscore data, and filters three-digit task records from unrelated lines.
What it assumes
Builds on basic Python strings and lists while introducing regex patterns and the re module from an overview.
What you can do after
Apply it via Python’s re module to build, test, and revise patterns for splitting, searching, and extracting text.
How it connects
Connects regex to Python string methods, raw strings, compiled patterns, Match objects, and named groups.

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 Python How-To. The section links point to the parts of the book it draws on.