Regex Examples and Cookbook

Large cookbook of validation, extraction, cleanup, and code-search regex recipes for everyday engineering work.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Validation Recipes

Practical validators and shape checks.

UUID shape

Match a standard hyphenated UUID.

regexANYvalidationuuid
regex
^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$

Covers UUID versions 1–5 and the standard variant bits.

IPv4 address

Match an IPv4 address with octet range checks.

regexANYvalidationipv4
regex
^(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}$

Avoids invalid octets like 999.

Domain name

Match a basic domain or subdomain name.

regexANYvalidationdomain
regex
^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,}$

Good for a simple domain shape check.

Hex color

Match CSS hex colors.

regexANYvalidationcsscolor
regex
^#(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$

Covers 3, 6, and 8 digit forms.

Semantic version

Match a semantic version string.

regexANYvalidationversions
regex
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$

Useful for package and release tooling.

ISO date shape

Match a simple YYYY-MM-DD date shape.

regexANYvalidationdate
regex
^\d{4}-\d{2}-\d{2}$

Shape validation only; calendar validity is a separate concern.

Extraction Recipes

Patterns that extract high-value substrings from larger text.

Extract URLs

Find basic HTTP or HTTPS URLs.

regexANYextractionurls
regex
https?://[^\s<>"']+

A pragmatic extractor for logs, markdown, and prose.

Extract emails

Find email-like tokens in text.

regexANYextractionemail
regex
\b[^\s@]+@[^\s@]+\.[^\s@]+\b

Useful for scanning text dumps and documents.

Capture a simple CSV field

Extract a non-quoted CSV field token.

regexANYextractioncsv
regex
[^,]+

Useful only for simple CSV; quoted CSV needs a more robust parser.

Extract fenced code block

Match a markdown fenced code block.

regexANYextractionmarkdowncode
regex
```(\w+)?
[\s\S]*?
```

Common when cleaning or indexing markdown content.

Extract log level

Capture common log levels from log lines.

regexANYextractionlogs
regex
\b(INFO|WARN|WARNING|ERROR|DEBUG|TRACE|FATAL)\b

Helpful for log triage and indexing.

Cleanup and Normalization

Regexes that clean or normalize messy text.

Match Windows line endings

Find CRLF line endings.

regexANYcleanupline-endings
regex
\r

Useful when normalizing files to LF.

Collapse blank lines

Match repeated blank lines.

regexANYcleanupwhitespace
regex
(?:\r?
\s*){3,}

Replace with `\n\n` or similar to reduce whitespace.

Match leading or trailing spaces per line

Trim each line in multiline mode.

regexANYcleanupwhitespace
regex
^\s+|\s+$

Use multiline mode to trim line edges.

Strip simple HTML-like tags

Match simple HTML tags for rough removal.

regexANYcleanuphtml
regex
</?[^>]+>

Okay for lightweight cleanup, not for full HTML parsing.

Normalize phone separators

Match common phone separators.

regexANYcleanupphone
regex
[-.\s()]

Useful when converting formatted phone numbers into digits-only values.

Redact card-like numbers

Find common 16-digit card-like sequences with separators.

regexANYcleanupsecurityredaction
regex
\b(?:\d{4}[- ]?){3}\d{4}\b

Use carefully and sanitize logs responsibly.

Language and Data Format Recipes

Regex patterns used with source code and structured text.

JSON key capture

Capture a simple JSON object key.

regexANYjsonparsing
regex
"([^"\\]+)"\s*:

Helpful for quick scans, though a parser is safer for full JSON.

YAML key shape

Match a simple YAML key at the start of a line.

regexANYyamlparsing
regex
^[A-Za-z0-9_.-]+:

Useful for lightweight audits of YAML-like files.

Find SELECT *

Find SQL `SELECT *` usage.

regexANYsqllinting
regex
(?i)\bselect\s+\*\s+from\b

Useful for database code cleanup.

Capture HTML id attribute

Capture an HTML `id` value.

regexANYhtmlattributes
regex
\bid=["\']([^"\']+)["\']

Useful for audits and DOM refactors.

CSS class selector

Match a simple CSS class selector.

regexANYcssselectors
regex
\.[A-Za-z_-][A-Za-z0-9_-]*

Useful for scanning stylesheets or extracting class tokens.

Environment variable assignment

Match `.env`-style key=value lines.

regexANYenvconfiguration
regex
^[A-Za-z_][A-Za-z0-9_]*=.*$

Useful for scanning or validating env files.

Recommended next

No recommendations yet.