Back to the blog

Developer

The patterns people ask for most.

Tested regex for email addresses, phone numbers, URLs, IP addresses, UUIDs and dates, with what each accepts. Try them in your browser in three engines.

By 6 min read

A developer toolkit reading encoded text in two steps, URL decode then Base64 decode, to give Hello, world!

A few patterns are asked for again and again: an email address, a phone number, a web address, an IP, a date. Each one copied from somewhere accepts something you did not expect. These are the patterns in KeepSimp's regex library, with what each accepts and refuses, and the tests they pass.

Email: the rule browsers use.

The HTML standard gives the pattern a browser checks an email field with, and it is the one in the library. It accepts ada@example.com and a.b+tag@sub.example.co.uk, and also x@localhost, because the standard allows a domain without a dot. It refuses ada@, @example.com, a space, and a domain that starts with a hyphen.

No pattern can tell whether an address exists or takes mail; only sending a message does that. Keep the pattern for catching typing slips.

Email address, as the HTML standard defines it
^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$

Phone numbers: E.164, or nothing.

^\+[1-9]\d{1,14}$ matches a number as E.164 writes it: a plus, a country code that does not start with 0, and at most 15 digits in all, with no spaces.

Numbers as people type them, with spaces, brackets, dashes and a local 0, differ too much from country to country for one pattern to judge. Take out the spaces and punctuation and check what is left, or use a library that knows each country's numbering plan.

Web addresses: a shape, then a parser.

^https?:\/\/[^\s/$.?#][^\s]*$ matches an http or https address with no spaces in it. It is a quick filter, not a judge. For anything stricter, hand the text to your language's URL parser, which reads addresses as the URL standard says.

IP addresses, UUIDs, dates and more.

The IPv4 pattern takes four numbers from 0 to 255 with no leading zeros. The IPv6 one takes the eight groups of RFC 4291, with :: for a run of zero groups; zone indexes such as %eth0 and IPv4 endings are left out. The UUID pattern takes versions 1 to 8 with RFC 9562's variant, in either case.

The date pattern checks the shape of an ISO 8601 date, not the calendar: 2026-02-31 passes, so check real dates with a date library. There are also patterns for an RFC 3339 date and time, a 24-hour time, a semantic version (semver.org's own), a hex colour, a URL slug, a JWT's shape, a word written twice, spaces at the ends of lines and a strong password.

A date's shape, from 2026-01-01 to 2026-12-31
^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$

Three engines, eight languages.

Open the regex tester and choose a pattern from the library: it comes with its tests, which pass in every engine it is marked for. JavaScript runs in your browser's engine, PCRE2 (the engine of PHP, R and nginx) runs as PCRE2 itself, and Go runs with RE2's rules. Patterns with lookaheads or backreferences, such as the password and repeated-word ones, are not marked for Go, which has neither.

The code panel writes the pattern for JavaScript, Python, Go, Java, C#, PHP, Rust and Ruby, escaped for each language's strings, with notes on what each engine reads differently. Before a pattern runs, the tester checks it for catastrophic backtracking, and a run that takes more than two seconds is stopped.

Further reading

Topics: email regex · phone number regex · url regex · ipv4 regex · uuid regex · date regex · regex cheat sheet · regex for validation

A little less theory. A little more doing.

Try it on your own terms, with a free browser tool.

Try the tool