Advertisement

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

PatternMeaning
^Start of string
$End of string
Word boundary — matches cat but not catch

Character Classes

PatternMeaning
.Any char except newline
d / DDigit / Non-digit
w / WWord char [A-Za-z0-9_] / Non-word
s / SWhitespace / Non-whitespace
[abc]Any of a, b, c
[^abc]Any except a, b, c

Quantifiers

PatternMeaning
* / *?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.
Advertisement