Skip to main content
The re module provides regular expression matching operations.

Module Import

Basic Matching

search() - Find First Match

match() - Match at Beginning

findall() - Find All Matches

finditer() - Iterator of Matches

Pattern Syntax

Common Patterns

Quantifiers

Substitution

sub() - Replace Pattern

sub() with Function

Groups and Capturing

Compiled Patterns

Flags

Practical Examples

Email Validation

Extract URLs

Phone Number Formatting

Compile patterns you use repeatedly for better performance.
Be careful with greedy quantifiers. Use ? for non-greedy matching: .*? instead of .*

string

String operations

Built-in Types

String type