Learn regular expressions · Lesson 14
Common patterns, and their traps
Some patterns get written over and over. Each looks simple, and each has a trap. Here they are with the trap first.
E-mail addresses
The rules for what an address may be (RFC 5322) allow quoted local parts, comments and characters no one uses; a pattern that follows them all is hundreds of characters long, and still can’t tell you whether the address exists. Check the shape, and let a confirmation mail check the rest:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/gm2 matchesalice@example.com not an address bob@mail.example.org charlie@localhostTry it in Dotstar →
(The m flag is only here to show several examples at once. When validating one form value, leave out both g and m: with m, ^ and $ match at every line, so junk\nalice@example.com\njunk would pass.)
Something, an @, something with a dot in it, no spaces. It accepts some invalid addresses, and deliberately rejects some valid but uncommon ones, such as charlie@localhost and quoted local parts ("alice smith"@example.com); for a form on a website, that’s usually the right trade.
Dates
[0-9]{4}-[0-9]{2}-[0-9]{2} checks the shape of an ISO date, and accepts 2026-13-45. ([0-9] rather than \d, which in several engines also takes other scripts’ digits.) A pattern can narrow the months and days, (?:0[1-9]|1[0-2]) for a month, but not tell that February has no 30th, or which years are leap years. Check the shape with a regex, then parse the date with your language’s date functions.
/\b[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])\b/g2 matches2026-10-24 2026-13-01 2026-02-30Try it in Dotstar →
The last one passes: the regex knows months and days, not calendars.
IP addresses
\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b finds things shaped like IPv4 addresses, including 999.1.1.1. It can also extract 1.2.3.4 from the longer 1.2.3.4.5, since there’s a word boundary between a digit and a dot. Limiting each part to 0–255 in the pattern is possible but long; treat a match as a candidate, and check the numbers and what surrounds it in code.
/\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/g3 matchesfrom 192.168.0.22 to 10.0.0.1, not 999.1.1.1Try it in Dotstar →
Log lines
Logs are where regexes shine: a fixed format, many lines, and the parts you want named. Anchor each line, and name the groups:
/^(?<time>\S+ \S+) (?<level>INFO|WARN|ERROR)\s+(?<message>.*)$/gm2 matches2026-10-24 09:12:01 INFO started 2026-10-24 09:15:20 ERROR disk fullTry it in Dotstar →
CSV, HTML, JSON: use a parser
[^,]+ splits a,b,c, and breaks on the first "Smith, Alice". A simple delimiter regex is not enough for CSV, because fields may contain commas, quotes and line breaks, and every program writes its own dialect. HTML and JSON add nesting and parsing rules that ordinary regex constructs cannot handle reliably. Some flavors support recursive patterns (PCRE2 and Perl, for example), but a dedicated parser is still clearer and safer, and every language has one for CSV, HTML and JSON. A regex is still fine for a quick search through such files, as long as nothing depends on it being exact.
When the pattern grows
A pattern that’s hard to read is hard to fix. Some ways to keep it readable:
- Build it from parts in code, each in a variable with a name that says what it is, and join them.
- Use the x flag where there is one (PCRE2, Perl, Python, Java, .NET, Ruby, Rust): it ignores spaces and allows
#comments in the pattern, so it can be laid out over lines. - Keep test cases beside it, strings that should match and ones that shouldn’t. Dotstar’s Tests tab runs them as you edit, and the Explain tab says what each part does.