Python regular expressions (re) basics
Python regular expressions (re) basics
Summary
Python's re module matches text against patterns. Write patterns as raw strings (r"..."). re.search finds a match anywhere, re.match only at the start, and re.fullmatch needs the whole string to match. findall, finditer and sub handle every match, and groups such as (?P<name>...) capture parts.
Draft. License: CC BY 4.0 (this summary). Sources: [S1] Python Software Foundation, "re: Regular expression operations (Python 3 docs)" (PSF License 2.0 docs; restated in our own words, no copied text); [S2] Python Software Foundation, "Regular Expression HOWTO (Python 3 docs)" (PSF License 2.0 docs; restated in our own words, no copied text).
Example
import re
m = re.search(r"(?P<y>\d{4})-(?P<m>\d{2})", "due 2026-11-02")
if m:
print(m.group("y"), m.group("m")) # 2026 11
re.sub(r"\s+", " ", "a b\n c") # 'a b c'
Facts
- id: F1 claim: Raw string literals (r'...') keep backslashes literal, so patterns like r'\d+' are not mangled by Python string escapes. confidence: medium date: 2026-11-02 sources: [S1, S2] license: CC BY 4.0 checked_on: 2026-11-02
- id: F2 claim: re.search() scans for the first match anywhere; re.match() only checks at the beginning; re.fullmatch() requires the entire string to match. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F3 claim: These functions return a Match object or None when nothing matches, so check the result before calling .group(). confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F4 claim: re.findall() returns all matches as strings, or tuples of groups if the pattern has more than one group; re.finditer() yields Match objects. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F5 claim: re.sub(pattern, repl, string) replaces matches; repl may refer to groups like \1 or be a function. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F6 claim: Parentheses create capturing groups; (?P<name>...) names a group, read with m.group('name'); (?:...) groups without capturing. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F7 claim: , + and ? are greedy; adding ? (?, +?, ??) makes them match as little as possible. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F8 claim: Flags such as re.IGNORECASE, re.MULTILINE (^ and $ per line) and re.DOTALL (. matches newline) change matching. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F9 claim: re.compile() makes a reusable pattern object; the module also caches recently used patterns. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
Caveats
- Regex is a poor fit for parsing HTML, JSON or other nested formats; use a parser.
- Badly written patterns can backtrack catastrophically on some inputs; keep patterns simple and anchored.
Pointers
- id: P1 answers: Full details and updates where: https://docs.python.org/3/library/re.html access: free, public web page
Source: https://docs.python.org/3/library/re.html · License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted), condensed and paraphrased; sources: Python Software Foundation, Python 3 documentation, re: Regular expression operations (PSF License 2.0); restated in our own words; Python Software Foundation, Python 3 documentation, Regular Expression HOWTO (PSF License 2.0); restated in our own words
Cite this
iinventedlight Library. 2026. "Python regular expressions (re) basics." iinventedlight Library, iil:note:nh5m2kyp, version of 2026-11-02. https://iinventedlight.com/library/reference/python-docs/python-regex-basics/. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/re.html.
@misc{iil_note_nh5m2kyp,
author = {{iinventedlight Library}},
title = {{Python regular expressions (re) basics}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-02},
url = {https://iinventedlight.com/library/reference/python-docs/python-regex-basics/},
note = {Permanent ID iil:note:nh5m2kyp. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/re.html.},
}
For language models and agents: start at /llms.txt. This page in Markdown: /library/reference/python-docs/python-regex-basics.md.