Regex Cheat Sheet: Quick Reference for Developers
A compact reference for regular expression syntax. Bookmark this page and use it alongside our Regex Tester for interactive testing.
Character Classes
| Pattern | Matches | Example |
|---|
. | Any character except newline | a.c → abc, a1c |
\d | Digit (0-9) | \d+ → 123 |
\D | Non-digit | \D+ → abc |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ → hello_1 |
\W | Non-word character | \W → @, # |
\s | Whitespace | \s+ → spaces, tabs |
\S | Non-whitespace | \S+ → hello |
[abc] | Any of a, b, or c | [aeiou] → vowels |
[^abc] | NOT a, b, or c | [^0-9] → non-digit |
[a-z] | Range a to z | [A-Za-z] → letters |
Quantifiers
| Pattern | Meaning | Example |
|---|
* | 0 or more | \d* → "", 123 |
+ | 1 or more | \d+ → 1, 123 |
? | 0 or 1 (optional) | colou?r → color, colour |
{3} | Exactly 3 | \d{3} → 123 |
{2,4} | 2 to 4 | \d{2,4} → 12, 1234 |
{2,} | 2 or more | \d{2,} → 12, 12345 |
*? | 0 or more (lazy) | ".+?" → shortest match |
+? | 1 or more (lazy) | ".+?" → shortest match |
Anchors
| Pattern | Matches |
|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
Groups and References
| Pattern | Purpose |
|---|
(abc) | Capture group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capture group |
\1 | Back-reference to group 1 |
(a|b) | Alternation (OR) |
Lookahead and Lookbehind
| Pattern | Name | Meaning |
|---|
(?=abc) | Positive lookahead | Followed by “abc” |
(?!abc) | Negative lookahead | NOT followed by “abc” |
(?<=abc) | Positive lookbehind | Preceded by “abc” |
(?<!abc) | Negative lookbehind | NOT preceded by “abc” |
Flags
| Flag | Name | Effect |
|---|
g | Global | Find all matches |
i | Case-insensitive | Ignore case |
m | Multiline | ^ and $ match line boundaries |
s | DotAll | . matches newlines |
u | Unicode | Enable \p{L} Unicode classes |
Common Patterns
Email: ^[^\s@]+@[^\s@]+\.[^\s@]+$
URL: https?://[^\s/$.?#].[^\s]*
IPv4: \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Date: \d{4}-\d{2}-\d{2}
HEX Color: #([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b
HTML Tag: <([a-z]+)[^>]*>(.*?)</\1>
Interactive testing: Paste any of these patterns into our Regex Tester to see matches highlighted in real time.
This article is part of our Regular Expressions Guide series.