Index Ai Academy
Phase 0 · ToolsPython core

Regular expressions

About 40 minLooking ahead: some prerequisites aren't done yet

Why this matters

Text arrives in every shape: log lines, phone numbers typed five different ways, product codes buried in emails, dates in messages. A regular expression describes the shape you're looking for in a few characters, and Python's re module finds, checks, extracts or replaces it. You'll use them to clean data, check input, read logs and pull fields out of model output.

  • Describe a text pattern with character classes, quantifiers and anchors
  • Pull pieces out of a match with groups and named groups
  • Use re.search, re.fullmatch, re.findall, re.finditer, re.sub and re.split for the right jobs
  • Know when a regular expression is the wrong tool

A pattern for text

A regular expression, or regex, is a small pattern language for text. Most characters match themselves; a few special ones describe kinds of text. \d means "any digit", and + means "one or more of the thing before". So \d+ matches a run of digits:

import re

line = "Order 50231: 2 tents, 1 stove, shipped in 3 days"
print(re.search(r"\d+", line).group())
print(re.findall(r"\d+", line))

re.search finds the first match and returns a match object. Its .group() is the text that matched. re.findall returns every match as a list of strings.

The r before each pattern makes it a raw string: Python leaves backslashes alone, so r"\d" reaches the re module as backslash-d. Without the r, some backslash pairs mean something else to Python first ("\b" is a backspace character). Always write patterns as raw strings.

The building blocks

PatternMatches
\done digit, 0 to 9
\wone "word" character: a letter, digit or underscore
\sone space, tab or newline
.any one character except a newline
[A-Z]one character from a set: here, a capital letter
[^0-9]one character not in the set
+one or more of the thing before
*zero or more
?zero or one: makes it optional
{3}exactly three; {2,4} means two to four

To match a special character itself, put a backslash before it: \. is a real dot and \$ a real dollar sign. The practice shop's product codes, like TE-101, are two capital letters, a hyphen and three digits: [A-Z]{2}-\d{3}.

import re

note = "Swap TE-101 for TE-104, and restock AP-122. Ignore te-999 and ABC-1234."
print(re.findall(r"[A-Z]{2}-\d{3}", note))
print(re.findall(r"\b[A-Z]{2}-\d{3}\b", note))

In plain words: read a pattern left to right, one piece at a time. [A-Z]{2}-\d{3} says "a capital letter, twice; then a hyphen; then a digit, three times". \b adds "and nothing word-like touching it".

The first pattern also finds "BC-123" hiding inside "ABC-1234". \b is a word boundary: the edge between a word character and anything else. With \b at both ends, the code must stand on its own, so the inside match is gone.