Skip to the content

Contact usFree tools · No signup · Files stay on your device

Developer Tools

Regex for email validation: patterns that actually work

Regex for email validation: patterns that actually work

Almost every sign-up form checks the email address with a regular expression, and almost every copy-pasted pattern either rejects real addresses or lets obvious rubbish through. The truth is that no short regex can follow the full email standard, and you do not need one. You need a pattern that catches typos, is easy to read, and cannot freeze your page.

This guide gives you three patterns, from loose to strict, with a table of what each one accepts. Every pattern can be pasted into the free Regex Tester, where you can try your own addresses before you ship anything. For a tour of the tester itself, see the Regex Tester guide.

What a regex can and cannot check

A pattern can check the shape of an address: some characters, an @, a domain with at least one dot. It cannot check that the domain exists or that the mailbox is real. [email protected] has a perfectly valid shape and goes nowhere.

So treat the regex as a first filter that saves the user from an obvious slip, and then send a confirmation message if the address matters. The pattern's job is to catch rahim@gmail and rahim.gmail.com, not to decide whether a person deserves an account.

Three patterns, from loose to strict

All three are written for whole-address checks. They start with ^ and end with $, so the entire text must be one address.

1. Simple

^[^\s@]+@[^\s@]+\.[^\s@]+$

It says: something that is not a space or @, then @, then something, a dot and something again. It is short and rarely rejects a real address. It also accepts things that are clearly wrong, such as [email protected] and [email protected].

2. Practical

^[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}$

The part before the @ uses letters, digits and . _ % + -. After it come one or more labels of letters, digits and hyphens, and the last label, the top-level domain, must be at least two letters. This is a sensible default for forms.

3. Stricter

^(?!\.)(?!.*\.\.)[A-Za-z0-9._%+-]+(?<!\.)@(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,}$

It adds four rules: no dot at the start of the address, no two dots in a row, no dot just before the @, and each domain label must start and end with a letter or digit and be at most 63 characters. The (?<!\.) part is a lookbehind, which current browsers support but some older ones do not. Test it on the devices you care about.

What each pattern accepts

These results come from running the three patterns on the same list.

Address Simple Practical Stricter
[email protected] yes yes yes
[email protected] yes yes yes
[email protected] yes yes yes
[email protected] yes yes yes
[email protected] yes yes yes
user@localhost no no no
@handle no no no
[email protected] no no no
space [email protected] no no no
[email protected] yes no no
[email protected] yes no no
john@exa_mple.com yes no no
[email protected] yes no no
[email protected] yes yes no
[email protected] yes yes no
[email protected] yes yes no
[email protected] yes yes no

Notice user@localhost. It can be a real address on an internal network, but none of the patterns accept it, because a public form almost never wants one. If you run an internal tool, relax the rule that demands a dot in the domain.

How to test a pattern in the tester

  1. Open Regex Tester and paste a pattern into the box between the slashes. Do not type the slashes yourself.
  2. In the Test string panel, replace the text with addresses, one per line.
  3. Turn on the g and m flags. The m flag makes ^ and $ work on each line, and g finds every line that matches.
  4. Read the highlights and the status line. With the practical pattern and the lines [email protected], bad@, [email protected] and imran@gmail, you get 2 matches: only the first and third lines are valid.
  5. To see the effect on one address only, turn m and g off and put a single address in the box.

Without the m flag, ^ and $ apply to the whole text, so a list of addresses will not match at all. For a real form field, which holds one address, you do not need m.

Finding emails inside text

Sometimes you need to pull addresses out of a paragraph, not validate a single one. Remove the anchors and turn on g and i. This is the pattern behind the tester's Email example button:

[\w.+-]+@[\w-]+(?:\.[\w-]+)+

On a text such as "Contact [email protected] or [email protected]." it finds both addresses and leaves the final full stop out. Put [email] in the Replace panel and the addresses are replaced by that word, which is a quick way to hide them before you share a log.

This pattern is looser on purpose. It would also match john@exa_mple.com, because \w includes the underscore. Use it for extraction, and use the anchored patterns for validation.

Mistakes that make email patterns fail

A trailing space or line break

A pasted address often has an invisible space or line break at the end. Because $ without the m flag does not match before a line break, [email protected] followed by a line break fails the practical pattern. Trim the value before you test it.

Forgetting the dot in the domain

[A-Za-z0-9-]+ does not contain a dot, so a domain with several parts needs the repeated group (?:\.[A-Za-z0-9-]+)*. Without it, [email protected] fails.

Limiting the top-level domain to three letters

Patterns like \.[a-z]{2,3}$ reject .info, .store and .agency. Use {2,} as in the patterns above.

Rejecting the plus sign

Many people use [email protected] to sort their mail. A pattern that leaves out + rejects them. All three patterns here accept it.

Case problems

The classes above contain both A-Z and a-z, so they accept capitals. If you write only a-z, either add the i flag or include A-Z.

Keep the pattern safe

Nested repeats can make a pattern take longer and longer on text that nearly matches. This is called catastrophic backtracking, and a form that uses such a pattern can be slowed down by a long string typed on purpose. Avoid shapes such as ^([a-zA-Z0-9]+[._-]?)+@, where a group with a quantifier is itself repeated. The patterns above do not nest quantifiers in this way.

The Regex Tester protects you while you experiment: if a pattern runs for more than 1.5 seconds, it stops it and shows "Pattern took too long". Press the Backtracking demo button to see how that looks. Your own code has no such protection, so test with long, nearly valid input before you publish.

What to do after the regex passes

  • Send a confirmation email with a link. It is the only real proof.
  • Show a gentle message for common typos, such as gmial.com. A regex cannot do this.
  • Store the address as typed. The domain part is not case sensitive, so lower-case it before you compare two addresses.
  • Never use the validation result as a security measure. It is a convenience for the user.

Quick checklist

  • Use the practical pattern as your default.
  • Anchor with ^ and $ for validation, and remove them for searching.
  • Allow + in the local part and a top-level domain of two or more letters.
  • Trim the input before you test it.
  • Test good and bad addresses together in the tester, then confirm by email.
  • Regex Tester: test and tune any of these patterns on your own addresses.
  • Text Diff Checker: compare a list of addresses before and after cleaning.

Frequently asked questions

Can a regex prove that an email address exists?
No. A pattern can only check the shape of the text, such as an @ sign and a domain with a dot. It cannot know whether the mailbox exists or belongs to the person typing. The only reliable proof is to send a message with a confirmation link.
Which pattern should I use in a sign-up form?
Start with the practical pattern in this guide, or even the simple one, and confirm by email. A very strict pattern mainly rejects real people with unusual but valid addresses, and it does not stop typos like gmial.com.
Why does my pattern fail when I paste an address with a space or line break at the end?
With anchors, the whole text must be the address. Without the m flag, $ does not match before a trailing line break, and a space is not allowed by the pattern. Trim the input before you test it.
How do I find emails inside a longer text?
Remove the ^ and $ anchors, turn on the g and i flags and use a pattern such as [\w.+-]+@[\w-]+(?:\.[\w-]+)+. The Email example button in the tester loads this one, with a text that shows a few near misses.
Are addresses with Bangla or other non-English letters valid?
Some systems accept them, but most do not, and the patterns here accept English letters, digits and the symbols . _ % + - before the @. If you need other scripts, turn on the u flag and add \p{L}\p{M}\p{N} to the allowed characters of the local part, after checking that your mail system really accepts them.
Found this useful? Share it

Tools in this guide

Main tool

Regex Tester

Write a regular expression and see every match highlighted as you type, with capture groups, named groups and a live replace preview.

Open tool

Text Diff Checker

Paste or open two versions of a text and see exactly what changed: added lines in green, removed lines in red.

Open tool

Back to all guides & blog