Skip to content

Instantly share code, notes, and snippets.

@txoof
Created August 20, 2025 09:41
Show Gist options
  • Save txoof/d666a192392bc67669b002946e05770c to your computer and use it in GitHub Desktop.
Save txoof/d666a192392bc67669b002946e05770c to your computer and use it in GitHub Desktop.
Python 3 RE Cheat Sheet

Regex Cheat Sheet (Python re)

Basics

Pattern Meaning
. Any character except newline (unless re.DOTALL)
^ Start of string (or start of line if re.MULTILINE)
$ End of string (or end of line if re.MULTILINE)
\A Start of string (ignores MULTILINE)
\Z End of string (ignores MULTILINE)
\b Word boundary
\B Not a word boundary
\\ Escape special character

Character Classes

Pattern Meaning
[abc] Any of a, b, or c
[^abc] Any character except a, b, or c
[a-z] Range: any lowercase letter
\d Digit [0-9]
\D Not a digit
\w Word character [a-zA-Z0-9_]
\W Not a word character
\s Whitespace (space, tab, newline, etc.)
\S Not whitespace

Quantifiers

Pattern Meaning
* 0 or more
+ 1 or more
? 0 or 1 (optional)
{n} Exactly n
{n,} At least n
{n,m} Between n and m
*?, +?, ??, {n,m}? Non-greedy versions

Groups

Pattern Meaning
(abc) Capture group
(?:abc) Non-capturing group
(?P<name>abc) Named group
(?P=name) Backreference by name
\1, \2 Backreference by group number
(?=abc) Positive lookahead
(?!abc) Negative lookahead
(?<=abc) Positive lookbehind (fixed-width)
(?<!abc) Negative lookbehind (fixed-width)

Alternation

Pattern Meaning
`a b`

Flags (inline and with re module)

Flag Inline Meaning
re.IGNORECASE (?i) Case-insensitive
re.MULTILINE (?m) ^ and $ match line boundaries
re.DOTALL (?s) . matches newlines
re.VERBOSE (?x) Allow whitespace/comments in pattern
re.ASCII (?a) \w, \d, \s match only ASCII
re.LOCALE (?L) Deprecated, locale-dependent classes
re.UNICODE (?u) Unicode matching (default in Python 3)

Escapes

Escape Meaning
\n, \r, \t Newline, carriage return, tab
\xhh Hex byte
\uhhhh Unicode code point (16-bit)
\Uhhhhhhhh Unicode code point (32-bit)
\ooo Octal value

Useful Functions in re

Function Use
re.match(pat, s) Match at start
re.fullmatch(pat, s) Match whole string
re.search(pat, s) First match anywhere
re.findall(pat, s) Return all matches (list of strings/tuples)
re.finditer(pat, s) Iterator of match objects
re.sub(pat, repl, s) Replace matches
re.split(pat, s) Split by regex
re.compile(pat, flags) Precompile regex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment