Home >Blog

Regex for Email Domain Validation: Examples and Common Pitfalls

Published Updated

Trying to validate an entire email address with a single regex is a trap. The local part alone has a half-dozen valid forms you'll never keep straight. But the part after the @ — the domain — is small and well defined, and a regex handles it cleanly. Here's the pattern that works and the steps to use it.

Why the one-regex approach fails

Addresses can contain plus tags, quotes, and a long list of allowed characters before the @, while the domain follows strict DNS rules. Lumping both into one expression gives you something unreadable that still rejects valid mail. Validate the domain separately and let the local part stay permissive.

The domain-only pattern

@([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])?)+)$

It reads as: an @, then one or more dot-separated labels. Each label starts and ends with a letter or digit, may contain hyphens in the middle, and stays within the 63-character DNS limit.

Use it, step by step

Step 1. Split at the @

Confirm there's exactly one @ and both sides are non-empty before you even bring out the domain regex.

Step 2. Match the domain against the pattern

import re

DOMAIN_RE = re.compile(
    r'@([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'
    r'(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+)$'
)

def domain_ok(email: str) -> bool:
    return bool(DOMAIN_RE.search(email.strip()))

Step 3. Reject the obvious failures separately

  • More than one @, or an @ at the very start or end.
  • A label that begins or ends with a hyphen, like -bad.com.
  • A TLD shorter than two characters or containing digits where it shouldn't.
A regex matching the separate domain segments of an email address

Pitfalls that keep coming up

  • Rejecting plus signs in the domain. Plus tags belong to the local part; don't let a copied pattern block valid addresses.
  • Hard-cating a TLD list. It goes stale the moment a new ending launches. Match the shape instead.
  • Treating regex as proof of deliverability. It isn't. A well-shaped address can still bounce.

Paste a few addresses into the regex tool and watch the domain light up separately from the local part. That separation is the whole point.

Questions, answered

Why not validate the whole email with one regex?
The local part before the @ has too many valid forms to track. Splitting it off and validating just the domain is a smaller, stable job.

Does this handle new TLDs like .shop or .online?
Yes. The pattern doesn't hard-code a TLD list, so longer and newer endings pass as long as they're valid labels.

Should I still send a confirmation email?
Yes. Regex only checks that an address is shaped correctly; only a confirmation proves the mailbox exists and belongs to the person.

What about international (IDN) domains?
They arrive as punycode starting with xn-- in most systems, which this pattern accepts. Decode them for display rather than validation.

Why is a label limited to 63 characters?
That's the actual DNS limit per label, so the bound reflects the spec rather than an arbitrary choice.

FAQ

Why not validate the whole email with one regex?
The local part before the @ has too many valid forms to track. Splitting it off and validating just the domain is a smaller, stable job.
Does this handle new TLDs like .shop or .online?
Yes. The pattern doesn't hard-code a TLD list, so longer and newer endings pass as long as they're valid labels.
Should I still send a confirmation email?
Yes. Regex only checks that an address is shaped correctly; only a confirmation proves the mailbox exists and belongs to the person.
What about international (IDN) domains?
They arrive as punycode starting with xn-- in most systems, which this pattern accepts. Decode them for display rather than validation.
Why is a label limited to 63 characters?
That's the actual DNS limit per label, so the bound reflects the spec rather than an arbitrary choice.

目次

情報

  • ヒット64
  • 公開日2026/09/29
0/500
敬意をもってご意見をお聞かせください。

もっと投稿

このセクションの記事をさらに探索
Regex for IP Addresses: Accurate IPv4 and IPv6 Validation Examples
Sep 09, 2026288ビュー

Regex for IP Addresses: Accurate IPv4 and IPv6 Validation Examples

Use regex to find possible IP addresses, then use the right validation rule for the job. This guide covers loo...

#regex for IP address#IP address regex#IPv4 regex#IPv6 regex
Password Validation Regex: Examples for Length, Numbers, Symbols, and Strong Passwords
Sep 07, 2026268ビュー

Password Validation Regex: Examples for Length, Numbers, Symbols, and Strong Passwords

Learn practical password validation regex patterns for length, digits, symbols, and mixed character classes. T...

#password validation regex#regex for password validation#strong password regex#regex lookahead
What Regex Should You Use for Phone Numbers? Patterns for US and International Formats
Aug 28, 2026256ビュー

What Regex Should You Use for Phone Numbers? Patterns for US and International Formats

Compare practical regex patterns for US formatted numbers, normalized local numbers, plus-prefixed internation...

#phone number regex#regex for phone number#US phone regex#international phone regex
How to Validate a URL with Regex: HTTP, HTTPS, Domains, and Query Strings
Sep 03, 2026248ビュー

How to Validate a URL with Regex: HTTP, HTTPS, Domains, and Query Strings

Build a practical HTTP and HTTPS URL regex by separating schemes, DNS domains, ports, paths, query strings, an...

#URL validation regex#regex for URL validation#HTTP URL regex#HTTPS URL regex
How to Extract Email Addresses from Text with Regex in Python and JavaScript
Sep 15, 2026211ビュー

How to Extract Email Addresses from Text with Regex in Python and JavaScript

Extract email addresses from any text with regex in Python and JavaScript: the pattern explained piece by piec...

#regex extract email#email regex python#email regex javascript#extract emails from text
Regex for Numbers Only: Integers, Decimals, Negative Values, and Leading Zeros
Sep 12, 2026211ビュー

Regex for Numbers Only: Integers, Decimals, Negative Values, and Leading Zeros

Choose the right regex for numbers-only input. This guide covers ASCII digits, integers, decimals, negative va...

#regex for numbers only#numbers only regex#integer regex#decimal regex
フィードバックメール