Regex for email addresses

The email pattern from the HTML standard, which is what every browser checks for <input type="email">, with what it lets through, what it turns away and what no regex can tell you.

//

Matches

This tool runs in your browser. We never send what you type or paste to a server.

The pattern

/^[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])?)*$/

This is the expression the HTML standard gives for a "valid email address", so it accepts exactly what a browser accepts in an <input type="email">. Using the same rule on the server means a form never passes the browser's check and then fails yours.

The tester at the top of this page is loaded with this pattern and the examples below, one per line. It adds the m flag so that ^ and $ apply to each line, and g to show every match. Edit either box to try your own input.

How it works, token by token

PartMeaning
^Start of the string.
[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+The local part, before the @: one or more letters, digits or any of the symbols .!#$%&'*+/=?^_`{|}~-. The / is escaped only because JavaScript writes the pattern between slashes, and the - is last so it is a literal hyphen rather than a range.
@A single literal @. Neither part can contain another one.
[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?The first domain label: it starts and ends with a letter or digit, may have hyphens in between, and is at most 63 characters long (1 + up to 61 + 1).
(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*Zero or more further labels, each after a literal dot (\.) and built the same way.
$End of the string.

What it matches and what it rejects

InputResultWhy
ana@example.commatchesThe everyday case.
first.last+tag@mail.example.co.ukmatchesDots and plus signs in the local part, several labels in the domain.
o'brien@example.iematchesApostrophes are allowed, and real addresses use them.
admin@localhostmatchesNo dot is needed after the @, so intranet names pass.
.ana..b@example.commatchesAccepted, although RFC 5322 does not allow a leading dot or two dots in a row.
ana.example.comrejectedNo @.
ana@@example.comrejectedTwo @ signs.
ana @example.comrejectedA space.
ana@-example.comrejectedA domain label cannot start with a hyphen.
ana@example-.comrejectedOr end with one.
ana@example..comrejectedAn empty label between two dots.
"ana b"@example.comrejectedQuoted local parts are legal in RFC 5322 but rejected here on purpose.
ana@exämple.comrejectedNon-ASCII domains must be written in their xn-- (Punycode) form.

Why not a full RFC 5322 pattern?

RFC 5322 defines the syntax of an address in an email message header, and it allows far more than anyone types into a sign-up form: quoted strings with spaces ("ana b"@example.com), comments in brackets, and folded whitespace. Patterns that try to cover all of it run to thousands of characters. The HTML standard calls its own rule "a willful violation of RFC 5322", because the RFC is too strict before the @, too vague after it and too lax about comments and quoting for what forms need.

The practical limits that matter come from RFC 5321, the SMTP standard: a local part can be at most 64 octets and a domain at most 255. The pattern above limits each label to 63 characters but not the totals, so add a plain length check if you store addresses in a fixed-size column.

What a regex cannot tell you

A pattern only checks the shape. ana@example.com passes and still bounces if nobody reads that mailbox, and ana@gmial.com passes too. The only proof that an address works is a message that arrives: send a confirmation link and treat the address as unverified until it is clicked.

Two smaller points help in practice. Trim spaces before checking, as browsers do: the standard strips leading and trailing whitespace from an email input before validating it. And do not lowercase the whole address when you store it. Domains are case-insensitive, but RFC 5321 says the local part may be case-sensitive, so lowercase only for comparisons such as "is this address already registered?".

A shorter pattern

If you only want to catch obvious typing mistakes, "something, an @, something, a dot, something" is easy to read and hard to get wrong:

/^[^\s@]+@[^\s@]+\.[^\s@]+$/
InputResultWhy
ana@example.commatchesFine.
x@y.zmatchesAny non-space characters either side of a dot.
ana@exämple.commatchesNon-ASCII is allowed.
admin@localhostrejectedNeeds a dot after the @.
ana @example.comrejectedNo spaces.
ana@@example.comrejectedOnly one @.

It is looser than the browser's rule in some places (any non-space character, including non-ASCII) and stricter in one: it insists on a dot after the @.

In JavaScript, Python, PHP and C#

JavaScript

const re = /^[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])?)*$/;
console.log(re.test('ana@example.com')); // → true
console.log(re.test('ana.example.com')); // → false

Python

import re

pattern = re.compile(r"[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])?)*")
print(bool(pattern.fullmatch('ana@example.com')))  # → True
print(bool(pattern.fullmatch('ana.example.com')))  # → False

PHP

<?php
$pattern = '/^[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])?)*$/D';
echo preg_match($pattern, 'ana@example.com'), "\n"; // → 1
echo preg_match($pattern, 'ana.example.com'), "\n"; // → 0

C#

using System;
using System.Text.RegularExpressions;

var pattern = new Regex(@"^[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])?)*\z");
Console.WriteLine(pattern.IsMatch("ana@example.com")); // → True
Console.WriteLine(pattern.IsMatch("ana.example.com")); // → False

Each version checks the whole string. JavaScript's $ only matches at the very end, but Python, PHP and .NET also let $ match just before a final line break, so "abc\n" could slip through. The Python version uses re.fullmatch (so the ^ and $ are dropped), PHP adds the D modifier, and C# ends with \z.

The JavaScript, Python and C# examples are run before each update and their output is compared with the comments. PHP is not installed on our build machine, so the PHP version is checked against the PCRE pattern modifier documentation, and its string literal is checked to hand PHP exactly the pattern tested here.

Questions

What is the best regex for email validation?

For web forms, the pattern from the HTML standard shown on this page: it is exactly what browsers enforce for input type="email", so the client and server agree. Then confirm the address by sending a message to it.

Does this pattern allow plus addressing such as name+tag@example.com?

Yes. The plus sign is in the allowed set for the local part, and many people rely on it to filter mail. Rejecting it is a common and avoidable bug.

Can an email regex check that the domain exists?

No. A regex sees only characters. To check the domain you would look up its MX or A records with DNS, and even then the mailbox may not exist. A confirmation email is the reliable test.