Regex Cookbook
Common regex patterns for emails, URLs, dates, validation, capture groups.
regexutility
# Regex Cookbook
## Anchors / classes
- `^` start, `$` end
- `\d` digit, `\D` non-digit
- `\w` word char, `\W` non-word
- `\s` whitespace, `\S` non-whitespace
- `.` any (except newline; `/s` flag for dotall)
## Quantifiers
- `*` 0+, `+` 1+, `?` 0 or 1
- `{n}`, `{n,}`, `{n,m}`
- Lazy: `*?`, `+?` (match as little as possible)
## Groups
- `(...)` capture
- `(?:...)` non-capture
- `(?<name>...)` named
- `\1` backreference
## Lookaround
- `(?=...)` lookahead, `(?!...)` negative lookahead
- `(?<=...)` lookbehind, `(?<!...)` negative lookbehind
## Common patterns
- Email (pragmatic): `^[\w.+-]+@[\w-]+\.[\w.-]+$`
- URL (rough): `^https?:\/\/[^\s/$.?#].[^\s]*$`
- ISO date: `^\d{4}-\d{2}-\d{2}$`
- IPv4: `^(?:\d{1,3}\.){3}\d{1,3}$`
- Hex color: `^#(?:[0-9a-f]{3}|[0-9a-f]{6})$`
- Slug: `^[a-z0-9]+(?:-[a-z0-9]+)*$`
- UUID: `^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`
## Tips
- Use `/i` for case-insensitive, `/g` for global, `/m` for multiline.
- For complex regex, use `/x` (extended) where supported or build with comments.
- Prefer parsers for HTML/JSON, not regex.
API: /api/skills/regex-cookbook