Regex Cheat Sheet: Essential Patterns for Text Processing
A practical regular expression reference covering the most commonly needed patterns for text processing. Includes email validation, URL matching, date parsing, and number formatting patterns with explanations.
Key Takeaways
- Regular expressions describe text patterns using a concise syntax.
- `*?`, `+?` — Non-greedy versions
- Catastrophic backtracking: Patterns like `(a+)+b` can take exponential time on inputs like `aaaaaaaaaaac`.
- ## Essential Patterns | Pattern | Matches | Example | | --- | --- | --- | | `\d{1,3}\.
Word Counter
Count words, characters, sentences, and paragraphs.
Regex Basics
Regular expressions describe text patterns using a concise syntax. They power search-and-replace in editors, input validation in forms, and text extraction in data pipelines. Mastering a handful of core patterns covers 90% of real-world regex needs.
Essential Patterns
| Pattern | Matches | Example |
|---|---|---|
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} |
IPv4 address (loose) | 192.168.1.1 |
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} |
Email (simple) | [email protected] |
https?://[^\s]+ |
URL (simple) | https://example.com/page |
\d{4}-\d{2}-\d{2} |
ISO date | 2026-03-10 |
#[0-9a-fA-F]{3,8} |
Hex color | #FF6B35 |
\b\w+\b |
Word boundary | Individual words |
Quantifiers and Groups
*— Zero or more (greedy)+— One or more (greedy)?— Zero or one (optional)*?,+?— Non-greedy versions{n,m}— Between n and m repetitions(abc)— Capture group(?:abc)— Non-capturing group(?=abc)— Lookahead (matches position before abc)(?<=abc)— Lookbehind (matches position after abc)
Common Mistakes
Catastrophic backtracking: Patterns like (a+)+b can take exponential time on inputs like aaaaaaaaaaac. Avoid nested quantifiers on the same characters.
Greedy matching: <.*> matches from the first < to the last > on a line, not individual tags. Use <.*?> (non-greedy) or <[^>]*> (character class).
Test and refine your regex patterns with the Peasy regex tester — real-time matching with step-by-step explanation of how the engine processes each pattern.