Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of 'user@example.co.uk' |
[a-zA-Z0-9._%+-]+ |
Character class matching one or more alphanumeric, dot, underscore, percent, plus, or hyphen characters (username part) | user, user.name, user+123 |
@ |
Literal '@' character | @ in 'user@example' |
[a-zA-Z0-9.-]+ |
Character class matching one or more alphanumeric, dot, or hyphen characters (domain name part) | example, sub.example |
\. |
Escaped literal dot (matches a single '.') | . |
[a-zA-Z]{2,} |
Character class matching two or more alphabetic characters (first TLD) | co, uk |
\. |
Escaped literal dot (matches a single '.') | . |
[a-zA-Z]{2,} |
Character class matching two or more alphabetic characters (second TLD) | uk, com |
$ |
Anchor: asserts position at end of the string | End of 'user@example.co.uk' |
Matches
user@example.co.ukname+tag@sub.domain.co.jpa@b.cd.ef123.456@789.abc.def
Does not match
user@example.comuser@.com.ukuser@example..com.ukuser@example.cuser@example.co.uk.@example.co.ukuser@.co.uk
Common pitfalls
- The pattern requires exactly two TLDs, so 'user@example.com' fails (only one TLD).
- The username part allows special characters like '%' and '+', which may not be valid in all email systems.
- The pattern does not enforce length limits, which could lead to overly long inputs.
- The domain part allows consecutive dots (e.g., 'user@sub..domain.co.uk'), which is invalid in practice.
- The pattern does not account for internationalized domain names (IDNs) or non-ASCII characters.
Performance notes
The pattern is generally efficient due to simple character classes and quantifiers. However, the use of multiple quantifiers (e.g., '+', '{2,}') could lead to catastrophic backtracking if the input is maliciously crafted (e.g., very long strings of allowed characters). Anchoring (^ and $) ensures the entire string is validated, which is good practice.