Regex Cheatsheet: Patterns Every Developer Should Know
Regular expressions are a mini-language for describing text patterns. They appear in every major programming language and in tools from grep to your code editor.
Free Regex TesterTest regular expressions against sample text with highlighted matches.
Open Regex Tester →Anchors
| Pattern | Meaning |
|---|---|
^ | Start of string |
$ | End of string |
| Word boundary — matches cat but not catch |
Character Classes
| Pattern | Meaning |
|---|---|
. | Any char except newline |
d / D | Digit / Non-digit |
w / W | Word char [A-Za-z0-9_] / Non-word |
s / S | Whitespace / Non-whitespace |
[abc] | Any of a, b, c |
[^abc] | Any except a, b, c |
Quantifiers
| Pattern | Meaning |
|---|---|
* / *? | 0 or more (greedy / lazy) |
+ / +? | 1 or more (greedy / lazy) |
? | 0 or 1 |
{n,m} | Between n and m times |
Groups and Lookaheads
(abc) — Capturing group (?:abc) — Non-capturing(?=...) — Positive lookahead (?!...) — Negative lookahead
Common Patterns
Email: [w.+-]+@[w-]+.[a-z]{2,}
IPv4: (?:d{1,3}.){3}d{1,3}
HEX color: #(?:[0-9a-fA-F]{3}){1,2}
Date YYYY-MM-DD: d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]d|3[01])
Greedy vs Lazy:
<.*> matches the longest string. <.*?> matches the shortest. Use lazy for HTML-like content.