$ webtoolist --read
Regex Cookbook
Ready-to-use regular expressions with examples — validation, extraction, and log parsing — plus the syntax you always forget.
Quick syntax refresher
| Token | Meaning |
|---|---|
^ / $ |
Start / end of line (or string without the m flag) |
\d \w \s |
Digit, word char [A-Za-z0-9_], whitespace |
+ * ? |
One or more, zero or more, optional (greedy) |
+? *? |
Lazy versions — stop at first match |
{2,4} |
Between 2 and 4 repetitions |
[abc] / [^abc] |
Character class / negated class |
(…) / (?:…) |
Capturing group / non-capturing group |
(?<name>…) |
Named group (PCRE, .NET, Python, JS ES2018+) |
\b |
Word boundary |
(?i) or i flag |
Case-insensitive |
Validation
# Email (practical, not RFC-complete)
^[^@\s]+@[^@\s]+\.[^@\s]{2,}$
# URL (http/https)
^https?:\/\/[\w.-]+\.[a-z]{2,}(\/\S*)?$
# Date — YYYY-MM-DD
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
# Time — HH:MM (24h)
^([01]\d|2[0-3]):[0-5]\d$
# Hex color — #RGB or #RRGGBB
^#?([0-9a-f]{3}|[0-9a-f]{6})$
# Password — 8+ chars, letter + digit
^(?=.*[A-Za-z])(?=.*\d).{8,}$
Extraction (use group 1)
# Domain from a URL https://docs.example.com/x → docs.example.com
https?:\/\/([^\/\s]+)
# Digits from a string a12b345 → 12 (then 345)
\d+
# ISO timestamp 2026-09-02T14:31:05Z
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z
# Query parameter value ?id=abc123 → abc123
[?&]id=([^&]+)
Log parsing
# Nginx access log line
^(\S+) \S+ \S+ \[([^\]]+)\] "(\S+) (\S+)[^"]*" (\d{3}) (\d+)
# group 1 = IP, 2 = timestamp, 3 = method, 4 = path, 5 = status, 6 = bytes
# IPv4 address
\b(?:\d{1,3}\.){3}\d{1,3}\b
# Log level from bracketed prefix [INFO] / [ERROR]
^\[[A-Z]{4,5}\]
# Stack-trace file:line refs
at .+\((\S+\.py):(\d+)\)
Common pitfalls
- Greedy matching eats too much:
".*"on{"a":"x"} {"b":"y"}grabs everything between the first and last quote. Use lazy.*?"or[^"]*. - Escaping: in
sed,\+and\?are GNU-isms; prefer-E(extended regex) and write+and?plainly. grepmatches lines; add-oto print only the matched part,-Pfor lookarounds ((?<=…)) on GNU systems.- Lookarounds
(?=…),(?!…),(?<=…),(?<!…)assert context without consuming it — great for “password must contain a digit” ((?=.*\d)). - Don’t regex HTML or JSON. They have grammars; use a real parser when nesting matters.