Skip to content

Beginning of the lesson

Regular Expressions in Python

Describe a text pattern once, then search, extract, validate, and replace with it

Big Idea: A regular expression (regex) is a small pattern language for describing the shape of text — "four digits", "an email-looking token", "a word followed by a comma". Python's built-in re module reads that pattern and finds every place in a string that matches.
By the end you will be able to:
  • read a pattern made of literals, metacharacters, character classes, and quantifiers;
  • use the core re functions — search, findall, sub, split, and compile;
  • capture pieces of a match with groups and named groups;
  • solve everyday tasks: validating, extracting, and cleaning text;
  • recognise when a regex is the wrong tool and reach for something simpler instead.

1. What is a regular expression?

Think of a regex as a stencil for text. A stencil is a card with a shape cut out of it; lay it over a page and only the parts that fit the hole show through. A regex is the same idea for strings: you describe a shape once, and the engine slides it across the text reporting every place the shape fits.

You have already searched text the blunt way with "needle" in haystack or str.find. Those only match an exact run of characters. A regex matches a family of strings: \d{4} matches any four digits — 1999, 2026, 0000 — without you listing them. That single idea is what makes regex powerful for phone numbers, dates, log lines, and file names.

In Python you reach this power through the standard-library module re. Nothing to install — just import re.

2. The re module: your toolbox

Five functions cover almost everything you will do. Each takes the pattern first and the text second.

FunctionWhat it doesReturns
re.search(p, s)Find the first place the pattern matches anywhere in sa Match object, or None
re.findall(p, s)Find all non-overlapping matchesa list of strings
re.sub(p, r, s)Replace every match with ra new string
re.split(p, s)Split s everywhere the pattern matchesa list of pieces
re.compile(p)Pre-build a pattern to reuse it many timesa Pattern object

A first taste — pull the first "word@word.word" token out of a sentence:

import re

text = "Contact ada@example.com for details."
m = re.search(r"\w+@\w+\.\w+", text)
print(m.group())

Output:

ada@example.com
Always use a raw string for patterns: r"\d+", not "\d+". The r stops Python from treating the backslash as an escape before re ever sees it. Without it "\b" becomes a backspace character and your pattern silently breaks.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers