Learn re

Regular expressions for searching, validating, extracting and rewriting text

15 lessons · 2 hours · 4 levels · covers re Python 3.13

The re module implements Perl-style regular expressions for Python strings and bytes. You use it to find patterns in text, validate formats, pull structured fields out of logs or documents, split on complex separators and perform rule-based replacements. Knowing it well also means knowing its limits: readability, Unicode details and catastrophic backtracking.

Before you start

re builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 45 min in total
2. PracticalUse it productively at work, with the usual idioms9 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs12 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know15 lessons · 2 hours in total

The re tree

research, match and fullmatchsearch, match andfullmatchCharacters, classes and anchorsCharacters, classesand anchorsQuantifiers and alternationQuantifiers andalternationCapturing groupsCapturing groupsfindall, finditer and splitfindall, finditer andsplitReplacing with subReplacing with subNamed groups and Match objectsNamed groups andMatch objectsFlags, compile and verbose patternsFlags, compile andverbose patternsGreedy vs lazy quantifiersGreedy vs lazyquantifiersFunctions as objects, lambdas and closuresFunctions as objects,lambdas and closur…Strings and f-stringsStrings and f-stringsIterators and generatorsIterators andgenerators
Essentials and Practical shown (9 of 15 lessons); the full list is below. Click a lesson to read it.

All 15 lessons, in order

Matching basics

  1. search, match and fullmatch · Essentials · 8 min

    Find a pattern anywhere with re.search, at the start with re.match, or require the whole string with re.fullmatch.

Pattern syntax

  1. Characters, classes and anchors · Essentials · 10 min

    Use ., character classes like [a-z] and \d, and anchors such as ^, $ and \b to describe what to match and where.

  2. Quantifiers and alternation · Essentials · 9 min

    Repeat parts of a pattern with *, +, ?, {m,n} and choose between alternatives with |.

  3. Flags, compile and verbose patterns · Practical · 10 min

    Change matching behaviour with IGNORECASE, MULTILINE, DOTALL and VERBOSE, and precompile patterns with re.compile.

  4. Greedy vs lazy quantifiers · Practical · 9 min

    Understand why .* overshoots, and use lazy quantifiers or negated classes to match the shortest text.

Extracting and replacing

  1. Capturing groups · Essentials · 9 min

    Capture parts of a match with parentheses and read them with group(n) and groups().

  2. findall, finditer and split · Essentials · 9 min

    Collect every match with findall or finditer, and split strings on patterns with re.split.

  3. Replacing with sub · Practical · 10 min

    Rewrite text with re.sub using backreferences, named groups and replacement functions.

  4. Named groups and Match objects · Practical · 9 min

    Name your groups with (?P<name>...) and use groupdict, span and lastgroup to work with matches cleanly.

Advanced patterns

  1. Lookahead and lookbehind · Advanced · 11 min

    Assert what comes before or after a position without consuming it, using (?=...), (?!...), (?<=...) and (?<!...).

  2. Unicode, bytes and case folding · Advanced · 10 min

    Know how str and bytes patterns differ, what \w and IGNORECASE mean for Unicode, and when to use re.ASCII.

  3. Writing a tokenizer with regex · Advanced · 12 min

    Combine named alternatives into one master pattern and use finditer with lastgroup to tokenize input.

Engine, performance and safety

  1. Catastrophic backtracking and ReDoS · Expert · 13 min

    Recognise patterns that take exponential time, and fix them with tighter classes, possessive quantifiers or atomic groups.

  2. How the re engine compiles and caches · Expert · 10 min

    Look inside re: parsing into opcodes, re.DEBUG output, the compiled-pattern cache and practical performance tips.

  3. Building patterns safely at runtime · Expert · 10 min

    Construct regexes from user input or word lists with re.escape, correct alternation order and size limits.