Recipes

Email Regex: A Practical Pattern and Its Validation Limits

Email Regex is excellent for learning structure, but a compact pattern should not be presented as a perfect implementation of every valid email address.

Before you start: test every pattern with examples that should match and examples that should fail.

A simple pattern for common cases

For learning and typical forms, a compact pattern can check local part, @, domain, and suffix.

Regex: ^[\w.-]+@[\w.-]+\.[A-Za-z]{2,}$\nMatches: dev@example.com\nRejects: dev@, @example.com

Understanding each part

[\w.-]+ accepts a sequence of word characters, dots, or hyphens. @ is literal. The domain follows a similar structure, and \. requires a literal dot before a suffix of at least two letters.

Why this is not universal email validation

The real email syntax contains edge cases that short teaching patterns deliberately omit. A syntactically plausible value also does not prove that the mailbox exists.

For real signups, combine reasonable format checking with email confirmation.

Test negative cases

Include missing usernames, missing domain suffixes, spaces, and malformed separators. Negative examples reveal over-permissive patterns quickly.

Next step: test it for real

Use the Playground to experiment with the pattern, then practice in the game with feedback on false positives and false negatives.

Practice in the gameTest in Playground

Keep learning

← Back to learning hub