---
title: "Python regular expressions (re) basics"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/re.html
source_license: "PSF License 2.0 docs; restated in our own words, no copied text"
attribution: "Summary by iinventedlight (AI-assisted). Source: Python Software Foundation, \"re: Regular expression operations (Python 3 docs)\" (PSF License 2.0 docs; restated in our own words, no copied text)."
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
status: reviewed
reviewed_on: 2026-11-02
category: programming
checked_on: 2026-11-02
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/re.html
source_license: "PSF License 2.0 (Python docs); restated in our own words, no copied text"
attribution: "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"
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
category: programming
tags: programming, python, regex, re, strings
checked_on: 2026-11-02
sources:
  - id: S1
    title: "re: Regular expression operations (Python 3 docs)"
    publisher: "Python Software Foundation"
    url: https://docs.python.org/3/library/re.html
    accessed: 2026-11-02
    license: "PSF License 2.0 docs; restated in our own words, no copied text"
    attribution: "Python Software Foundation, Python 3 documentation, re: Regular expression operations (PSF License 2.0); restated in our own words"
  - id: S2
    title: "Regular Expression HOWTO (Python 3 docs)"
    publisher: "Python Software Foundation"
    url: https://docs.python.org/3/howto/regex.html
    accessed: 2026-11-02
    license: "PSF License 2.0 docs; restated in our own words, no copied text"
    attribution: "Python Software Foundation, Python 3 documentation, Regular Expression HOWTO (PSF License 2.0); restated in our own words"
---
# 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
```python
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
