Regex patterns, explained
24 regular expressions worth knowing — what each one is for, how it works part by part, what it accepts and rejects, and where it will bite you. Open any of them in Rex to try it on your own text.
Validation
Is this string the shape it claims to be — an email, a URL, an IP address?
- Email address
/^[\w.+-]+@[\w-]+\.[\w.-]+$/A pragmatic email check — the one worth using, not the one that is technically complete.Read the breakdown - HTTP/HTTPS URL
/^https?://[^\s/$.?#][^\s]*$/Accepts http and https URLs without pretending to parse every corner of the spec.Read the breakdown - IPv4 address
/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/Four numbers, each genuinely 0–255 — not the \d{1,3} version that accepts 999.Read the breakdown - UUID (version 1–5)
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iThe canonical 8-4-4-4-12 form, with the version and variant digits actually checked.Read the breakdown - Hex colour code
/^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/iThree or six hex digits after a hash — and why the six-digit branch must come first.Read the breakdown - Semantic version
/^\d+\.\d+\.\d+(?:-[\w.]+)?(?:\+[\w.]+)?$/major.minor.patch, with optional prerelease and build metadata.Read the breakdown - URL slug
/^[a-z0-9]+(?:-[a-z0-9]+)*$/Lowercase words joined by single hyphens — no leading, trailing or doubled dashes.Read the breakdown - Password strength rules
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}$/Four independent lookaheads, each asking "is there one of these anywhere?".Read the breakdown - E.164 phone number
/^\+[1-9]\d{7,14}$/The international format: a plus, a non-zero country code, then digits.Read the breakdown
Text & extraction
Find, pull apart and clean up text: tags, links, quoted strings, repeated words.
- Trim leading and trailing whitespace
/^\s+|\s+$/gmTwo anchored alternatives in one pass — the classic trim, per line.Read the breakdown - Repeated word
/\b(\w+)\s+\1\b/giA backreference catches the the kind of typo a spellchecker walks straight past.Read the breakdown - Quoted string with escapes
/"[^"\\]*(?:\\.[^"\\]*)*"/gThe unrolled loop — matches "a \"quoted\" string" without catastrophic backtracking.Read the breakdown - HTML tag
/</?([a-z][\w-]*)[^>]*>/giFinds tags in a blob of markup — useful for stripping, dangerous for parsing.Read the breakdown - Markdown link
/\[([^\]]+)\]\(([^)]+)\)/gTwo captures — the label and the target — with negated classes instead of lazy dots.Read the breakdown - camelCase boundary
/([a-z0-9])([A-Z])/gTwo captures around the seam, so a replacement can insert a hyphen between them.Read the breakdown - Words in a text
/\b[\w']+\b/gCounting what a human calls a word, apostrophes included.Read the breakdown - Leading zeros
/^0+(?=\d)/gmA lookahead keeps the last digit safe, so 000 becomes 0 rather than nothing.Read the breakdown
Numbers
Integers, decimals, money — and the positions between digits.
- Whole number
/^-?\d+$/The smallest useful validator — and a reminder of what the anchors are doing.Read the breakdown - Decimal number
/^-?\d+(?:\.\d+)?$/A number with an optional fractional part — note what the ? applies to.Read the breakdown - Currency amount
/^\$?\d{1,3}(?:,\d{3})*(?:\.\d{2})?$/Thousands separators in groups of three, with exactly two decimal places.Read the breakdown - Thousands separator positions
/\B(?=(\d{3})+(?!\d))/gMatches nothing at all — just the positions where a comma belongs.Read the breakdown
Dates & times
Calendar dates, clock times, and the log lines they turn up in.
- ISO date (YYYY-MM-DD)
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/Month and day ranges enforced by alternation, not by \d{2}.Read the breakdown - 24-hour time
/^(?:[01]\d|2[0-3]):[0-5]\d$/Hours 00–23 and minutes 00–59, each range built from digit classes.Read the breakdown - Log line timestamp
/^\[(\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2})\]/gmPulls the timestamp out of a bracketed log line, one capture group.Read the breakdown
Have a pattern of your own?
Paste it into Rex to see what it matches, read it back in plain English, and find out whether its cost explodes as the text gets longer.
Open Rex