Regex Cheat Sheet: Common Patterns & Syntax
A quick reference and guide for Regular Expressions (Regex). matching characters, quantifiers, groups, lookarounds, and flags.
Introduction
Regular Expressions (Regex) are a sequence of characters that define a search pattern. They are the Swiss Army knife of text processing, allowing you to validate forms, scrape data, or perform complex find-and-replace operations.
While the syntax can look cryptic (^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$), mastering just a few concepts gives you superpowers.
Character Classes
Character classes match a specific type of character.
| Pattern | Matches | Example |
|---|---|---|
. | Any character (except newline) | a.c matches "abc", "a@c" |
\d | Any digit (0-9) | user_\d matches "user_5" |
\D | Any non-digit | \D+ matches "abc" |
\w | Alphanumeric + underscore | \w+ matches "User_123" |
\W | Non-alphanumeric | \W matches "!" or "@" |
\s | Whitespace (space, tab, newline) | hello\sworld |
\S | Non-whitespace | \S+ matches "word" |
Anchors
Anchors don't match characters; they match positions.
| Pattern | Matches |
|---|---|
^ | Start of the string (or line in multiline mode) |
$ | End of the string (or line in multiline mode) |
\b | Word boundary (start/end of word) |
\B | Non-word boundary |
Example:
^hellomatches "hello world" but not "oh hello".world$matches "hello world" but not "world peace".
Quantifiers
Quantifiers specify how many times the previous character/group should repeat.
| Pattern | Meaning | Example |
|---|---|---|
* | 0 or more | a*b matches "b", "ab", "aaaaab" |
+ | 1 or more | a+b matches "ab", "aaab" (not "b") |
? | 0 or 1 (Optional) | colou?r matches "color", "colour" |
{n} | Exactly n times | \d{4} matches "2026" |
{n,} | n or more times | \d{2,} matches "12", "12345" |
{min,max} | Between min and max | \w{3,8} matches "User", "Username" |
Greedy vs Lazy:
By default, quantifiers are greedy—they match as much as possible. Adding a ? makes them lazy.
- String:
<div>content</div> <.+>(Greedy): matches<div>content</div>(entire string)<.+?>(Lazy): matches<div>and</div>separately.
Groups and Lookarounds
Capturing Groups (...)
Groups parts of the regex and captures the match for later use (e.g., in replacement).
- Pattern:
(\d{4})-(\d{2})on "2026-01" - Group 1: "2026"
- Group 2: "01"
Non-Capturing Groups (?:...)
Groups for logic/quantifiers but doesn't capture the result.
(?:ma)+matches "mama" but doesn't create a capture group.
Lookahead (?=...)
Asserts that what follows matches the pattern, but doesn't consume characters.
d+(?= dollars)matches "100" only in "100 dollars".
Lookbehind (?<=...)
Asserts that what precedes matches the pattern.
(?<=USD)d+matches "100" only in "USD100".
Flags
Flags change how the regex engine behaves. They are appended to the end (e.g., /pattern/gi).
| Flag | Name | Description |
|---|---|---|
g | Global | Match all occurrences, don't stop at first. |
i | Insensitive | Case-insensitive matching. |
m | Multiline | ^ and $ match start/end of lines, not just string. |
s | Dotall | . matches newlines as well. |
u | Unicode | Enable full Unicode support. |
Common Patterns
Email Address (Simplified)
^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$
URL (HTTP/HTTPS)
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])
Conclusion
Regex is a language of its own. It's often write-only (hard to read later), so commenting your complex regexes or splitting them up is good practice. Use our Regex Tester to debug your patterns in real-time with visual highlighting!