Skip to main content
Regular expressions (called REs, regexes, or regex patterns) are a powerful tool for matching text patterns in Python using the re module.

Getting Started

Compiling Patterns

Compile a regular expression pattern into a pattern object for reuse:

Basic Matching

Match patterns at the beginning of strings:

Pattern Syntax

Character Classes

Match specific sets of characters:
  • [abc] - matches ‘a’, ‘b’, or ‘c’
  • [a-z] - matches any lowercase letter
  • [^5] - matches any character except ‘5’

Special Sequences

Pre-defined character sets:
  • \d - any decimal digit [0-9]
  • \D - any non-digit [^0-9]
  • \s - any whitespace character
  • \S - any non-whitespace character
  • \w - any alphanumeric character [a-zA-Z0-9_]
  • \W - any non-alphanumeric character
Always use raw strings (prefix with r) for regex patterns to avoid backslash issues:

Repetition

Specify how many times to match:
  • * - 0 or more times
  • + - 1 or more times
  • ? - 0 or 1 time
  • {m,n} - at least m, at most n times

Searching and Finding

Grouping

Capture parts of the pattern:

Named Groups

Use names instead of numbers:

Backreferences

Match repeated patterns:

String Modification

Splitting

Split strings on pattern matches:

Substitution

Replace pattern matches:

Compilation Flags

Modify pattern behavior:

Common Patterns

Email Validation

Phone Numbers

URL Extraction

Best Practices

Performance Tip: Compile patterns that are used multiple times:
When NOT to Use Regex: For simple string operations, use string methods:

Common Gotchas

Greedy vs Non-Greedy

By default, repetition is greedy:

Anchors Matter

Reference

Key re module functions:
  • compile(pattern, flags=0) - Compile a pattern
  • match(pattern, string, flags=0) - Match at string start
  • search(pattern, string, flags=0) - Search anywhere
  • findall(pattern, string, flags=0) - Find all matches
  • sub(pattern, repl, string, count=0, flags=0) - Replace matches
  • split(pattern, string, maxsplit=0, flags=0) - Split on pattern