Regex
Python · Reference cheat sheet
Regex
Python · Reference cheat sheet
📋 Overview
The re module provides regular expressions for search, match, split, and substitute. Prefer raw strings (r"..."). For complex grammars, consider parsers; for fixed substrings, prefer str methods.
🔧 Core concepts
| API | Role |
|---|---|
re.search | First match anywhere |
re.match | Match at start |
re.fullmatch | Entire string |
re.findall / finditer | All matches |
re.split | Split by pattern |
re.sub | Replace |
re.compile | Reuse pattern |
| Flags | IGNORECASE, MULTILINE, DOTALL, VERBOSE |
Groups: (...) capturing, (?:...) non-capturing, (?P<name>...) named. Conditional patterns exist but stay rare.
💡 Examples
Search and groups:
import re
text = "user=ada id=42"
m = re.search(r"user=(?P<user>\w+)\s+id=(?P<id>\d+)", text)
if m:
print(m.group("user"), int(m.group("id")))Compile and findall:
import re
email = re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+")
print(email.findall("a@x.com b@y.org"))sub and backrefs:
import re
raw = "2024-07-10"
print(re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\2/\3/\1", raw))VERBOSE + flags:
import re
pattern = re.compile(
r"""
^
(?P<year>\d{4}) -
(?P<month>\d{2}) -
(?P<day>\d{2})
$
""",
re.VERBOSE,
)
print(pattern.fullmatch("2024-07-10").groupdict()) # type: ignore[union-attr]⚠️ Pitfalls
- Catastrophic backtracking on crafted input — avoid nested ambiguous quantifiers on untrusted data.
re.matchonly checks the start — usefullmatchorsearchintentionally.- Forgetting raw strings:
"\n"vsr"\n"/"\\d"mistakes. str.split/replaceis clearer for fixed delimiters.- Bytes patterns require
bytesinput — don't mix str/bytes.