Regular expressions, pattern by pattern
Regex patterns for the data people actually validate (emails, phones, dates, URLs) and the syntax behind them (classes, quantifiers, groups, lookarounds, flags), each with the direct answer, tested patterns, matches and non-matches, pitfalls, and the Regexdrill exercises that practice it with live highlighting.
40 patterns, each with the direct answer, syntax, worked examples, the mistakes people make and the drills that practice it. Updated November 2026.
Patterns for common data
- Regex for emailA practical email regex for JavaScript and other engines, how to validate a whole field, extract addresses from text, and why RFC-perfect patterns are a trap.
- Regex for phone numberPhone number regex for US numbers with dashes, dots, spaces or parentheses, an E.164 check for international input, and how to normalize before validating.
- Regex for date YYYY-MM-DDA regex for ISO dates like 2024-03-15 with month and day ranges, named groups for year, month and day, reformatting MM/DD/YYYY, and what a regex cannot check.
- Regex for URLA URL regex for validation and for pulling links out of text, capturing scheme, host, port and path, avoiding trailing punctuation, and when to use new URL.
- Regex for IPv4 addressA loose and a strict IPv4 regex, how the 0 to 255 alternation works, how to find IP addresses in log text, and the cheaper split-and-compare alternative.
- Regex for passwordA password regex built from one lookahead per rule plus a length check, how (?=.*X) works, how to report which rule failed, and the mistakes that weaken it.
- Regex for ZIP codeZIP code regex for US 5-digit and ZIP+4 formats, plus patterns for UK, Canadian and German postal codes, with examples and the common anchoring mistake.
- Regex for hex colorA hex color regex for 3 and 6 digit CSS codes, an extended one for alpha, pulling colors out of a stylesheet, and converting a match to RGB numbers.
- Regex for usernameUsername regex patterns: the basic ^[a-zA-Z0-9_]{3,16}$ rule, a stricter letter-first variant, pulling @handles out of text, and a Unicode-friendly version.
- Regex for credit cardCredit card regex by brand (Visa, Mastercard, Amex, Discover), stripping spaces first, why you still need the Luhn check, and masking all but the last four.
- Regex for time HH:MMA time regex for 24-hour HH:MM from 00:00 to 23:59, a 12-hour version with AM or PM, how to add optional seconds, and how to pull times out of log lines.
- Regex for UUIDA UUID regex for the 8-4-4-4-12 format with the i flag, a version 4 only variant, a pattern that accepts missing dashes, and how to extract a UUID from a URL.
- Regex for whitespaceHow \s matches spaces, tabs and newlines, trimming leading and trailing whitespace, collapsing repeated spaces, splitting on whitespace, and detecting blanks.
- Regex for numbers onlyThe ^\d+$ pattern for digits-only strings, stripping everything that is not a digit, extracting numbers from text, and patterns for signed and decimal values.
- Regex for letters onlyThe ^[a-zA-Z]+$ pattern for ASCII letters, a Unicode version with \p{L} and the u flag, allowing spaces between words, and stripping non-letters from text.
- Regex for alphanumericThe ^[a-zA-Z0-9]+$ pattern, how it differs from \w, how to make a URL slug by replacing everything else, and a Unicode version with \p{L} and \p{N}.
- Regex for HTML tagsHow to remove HTML tags with <[^>]+>, list tag names, read an href value, match the content of one tag lazily, and why regex is not an HTML parser.
Syntax
- Character classesWhat a character class matches, how ranges and negation work, what \d \w \s and their uppercase opposites mean, and what the dot does and does not match.
- QuantifiersWhat * + ? {n} {n,} and {n,m} mean, how greedy matching works, how to repeat a group instead of a single character, and the silent {2, 3} mistake.
- Anchors ^ and $What ^ and $ match, how they change with the m flag, whole-string versus per-line matching, and the mistakes with alternation and trailing newlines.
- Alternation |How the pipe works, why ^cat|dog$ does not do what it looks like, how to group alternatives, and why option order matters when one is a prefix of another.
- Lazy vs greedyHow greedy quantifiers overshoot, how adding ? makes them lazy, why a negated class is often better than .*?, and the performance and newline traps.
- Word boundary \bWhat \b matches, how to find or replace whole words only, what \B does, and why \b fails next to punctuation, in string-built regexes and with Unicode text.
- Escaping special charactersThe regex metacharacters that must be escaped, how to match a literal dot, dollar, plus or slash, what changes inside brackets, and how to escape user input.
- Dot and newlineWhy . does not match line breaks by default, the s flag in JavaScript and re.DOTALL in Python, the portable [\s\S] trick, and how s differs from m.
- Negated character classHow [^...] matches any character except the listed ones, stripping non-digits, grabbing quoted text, splitting on a delimiter, and how it differs from (?!).
- Optional with ?How ? makes a character optional, making a whole sequence optional with a group, what the capture holds when the group is absent, and the ? that means lazy.
- Ranges in character classesHow character ranges work by code point, how to combine them, the invalid [a-Z] and surprising [A-z], and how to express numeric ranges with alternation.
- Unicode regexHow the u flag changes JavaScript regex, matching any letter with \p{L}, a script with \p{Script=...}, emoji as one character, and ASCII limits of \w and \b.
Groups and lookarounds
- Capture groupsHow parentheses capture text, reading groups in JavaScript via match, matchAll and exec, reusing them with $1 in replace, and the g trap that loses groups.
- Non-capturing group (?:...)How (?:...) groups for quantifiers and alternation without a numbered capture, why that keeps group numbers stable, and how it changes findall in Python.
- Named groups (?<name>...)How to capture by name, read match.groups.name, use $<name> in replacements and \k<name> as a backreference, and the (?P<name>) syntax Python needs.
- Backreferences \1What \1 means, finding doubled letters and repeated words, matching a closing quote equal to the opening one, and \1 in a pattern versus $1 in a replacement.
- Lookahead (?=...) and (?!...)How lookahead checks what follows without consuming it: numbers before a unit, words without a prefix, thousands separators and file name filters.
- Lookbehind (?<=...) and (?<!...)How lookbehind checks what precedes the match, grabbing the number after a dollar sign or excluding negatives, browser and Python support, and width limits.
Flags and tools
- Regex flagsWhat each JavaScript regex flag does (case-insensitive, global, multiline, dotAll, unicode, sticky), how to set them, and the lastIndex trap with g and test.
- match, test, replace in JavaScriptWhich JavaScript regex method to use for a yes/no check, the first match, all matches with groups, substitution and splitting, with return values and g rules.
- Regex in Python (re)How the Python re module works: search versus match versus fullmatch, findall and its group behavior, sub for replacement, raw strings, and flags.
- Regex in ExcelThe native regex functions in Microsoft 365 Excel to test, extract and replace text, the return_mode and case options, Google Sheets equivalents, and VBA.
- Regex in SQLRegular expressions in SQL by database: PostgreSQL ~ and regexp functions, MySQL REGEXP and REGEXP_REPLACE, Oracle REGEXP_LIKE, and what SQL Server offers.
Reading is not practicing
Every pattern here links to drills that check your answer on live data. All of them are free to try.
Start practicing