Regex Cookbook
Battle-tested regular expressions for the tasks developers do every week — validation, extraction, cleanup — with ready-to-paste snippets in JavaScript, Python, Go, and Java.
Every entry explains the pattern piece by piece, shows real matches and non-matches, and calls out the common pitfall that catches most people — the reason your pattern “works but misses one case.” When regex is not the right tool at all (URL parsing, phone numbers, HTML), we say so and point at the language-native alternative.
Try any pattern live in our Regex Tester.
Validation
Verify that input matches an expected format — email, URL, IP, UUID, password strength.
Match an email address
[\w.+-]+@[\w-]+\.[\w.-]+Match a URL
https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*Match an IPv4 address
(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)Match an IPv6 address
(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::Match a UUID (v4)
[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}Match a US phone number
(?:\+?1[-. ]?)?\(?[2-9][0-9]{2}\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}Match a US ZIP code
\b[0-9]{5}(?:-[0-9]{4})?\bMatch a MAC address
(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}Match a CSS hex color
#(?:[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{3})\bValidate a strong password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$Extraction
Pull specific pieces of information out of a larger text: hashtags, mentions, query params.
Text
Normalize and clean text: collapse whitespace, remove empty lines, catch duplicate words.
Dates & Times
Match ISO 8601 dates, timestamps, and 24-hour times.
Numbers
Integers, floats, scientific notation, and hexadecimal literals.
Web
Domain names and other web-specific tokens.
Code
Match programming identifiers (camelCase, PascalCase, snake_case) and comments.