Regex Recipes
Common, production-ready regex patterns you can reuse across projects.
Common Patterns
| Key / Code | Description |
|---|---|
| ^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$ | |
| URL | https?:\/\/[\w.-]+(?:\/[\w./?%&=-]*)? |
| IPv4 | ^(?:\d{1,3}\.){3}\d{1,3}$ |
| UUID v4 | ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$ |
| ISO Date | ^\d{4}-\d{2}-\d{2}$ |
| Hex Color | ^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$ |
Text Utilities
| Key / Code | Description |
|---|---|
| Slug | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
| SemVer | ^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$ |
| Base64 | ^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$ |
Tips
Regex is context-sensitive. Always validate against your inputs and consider escaping for your runtime (JS, Python, Go).
When Recipes Help Most
These patterns are most useful when you need a fast starting point for validation rules, extraction tasks, or test fixtures. They should be treated as templates: verify edge cases against your own runtime, language escaping rules, and actual user input before shipping them.
Common Recipe Mistakes
The biggest mistake is copying a pattern from a cheatsheet and assuming it covers every real-world case. Email addresses, URLs, and version strings often have edge cases beyond the minimal recipe. Another issue is forgetting to anchor the pattern with ^ and $ when full-string validation is required.