Email Validation Regex in Python That Accepts Real Addresses

An email check that is too strict quietly drops real customers - plus-addressing, subdomains, hyphenated domains and long TLDs all fail. Loosen the Python regex so every real address passes and malformed ones are still rejected.

Start this project - freeLive cloud workspace in your browser · lands on your portfolio
Data Engineerpythonregexdata-validation

The too-strict pattern

pattern = r"^[a-zA-Z0-9._]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$"

This rejects:

A practical pattern

import re

EMAIL = re.compile(r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}$")

def validate_email(value):
    return bool(EMAIL.match(value.strip()))

It allows the characters real addresses use, any number of subdomains and any TLD length, while the ^ and $ anchors still reject spaces, a second @, or an empty side.

Test both directions

A validation change needs two test sets: real addresses that must pass, and malformed ones that must still fail. Loosening a pattern until everything passes is just as wrong as the original bug.

Regex is a filter, not proof

No regex can tell you an address exists. In a pipeline, use a sensible pattern to catch obvious garbage, and treat a confirmation email (or a bounce) as the real check. Over-strict validation costs more than it saves.

What you do in the project

You run the import, see 7 of 10 valid customers rejected, change the pattern, and confirm all 10 pass while every address in the invalid file still fails.

Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.

Start this project - free

What you'll practice

FAQ

What is a good regex for email validation in Python?

A practical pattern is ^[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}$. It accepts plus-addressing, subdomains, hyphens and long TLDs, and rejects missing parts, spaces and double @.

Can a regex fully validate an email address?

No. The full email specification is too complex for a practical regex, and a valid format does not mean the mailbox exists. Use a reasonable pattern, then confirm by sending an email.

Why do plus signs in emails fail validation?

Many hand-written patterns only allow letters, digits, dots and underscores before the @. Plus-addressing (name+tag@domain) is valid and common, so the pattern must allow +.

Keep learning

Validate Data Before Loading (Python ETL)Data projectHow to Handle Bad CSV Data in PythonData projectHow to Validate DataFrames With panderaData projectData roadmapStep by step to hiredData interview questionsSTAR answersAll Data projectsProjects hub

Do this project yourself. It's ready in a live cloud workspace in your browser - when you finish, it lands on your portfolio.

Start this project - free
Start this project - free