Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts the position at the start of the string | Start of 'user@example.com' |
[ |
Start of a character class | N/A |
\w |
Matches any word character (letters, digits, underscore, or hyphen) | a-z, A-Z, 0-9, _ |
\. |
Matches a literal dot character | . |
\- |
Matches a literal hyphen character | - |
] |
End of the character class | N/A |
+ |
Quantifier: matches one or more of the preceding token (character class) | user, user.name, user-name |
@ |
Matches a literal '@' symbol | @ |
[ |
Start of a character class | N/A |
\w |
Matches any word character (letters, digits, underscore, or hyphen) | a-z, A-Z, 0-9, _ |
\. |
Matches a literal dot character | . |
\- |
Matches a literal hyphen character | - |
] |
End of the character class | N/A |
+ |
Quantifier: matches one or more of the preceding token (character class) | example, sub.example, sub-example |
\. |
Matches a literal dot character | . |
[ |
Start of a character class | N/A |
a-zA-Z |
Matches any uppercase or lowercase letter | a, B, z |
] |
End of the character class | N/A |
{2,} |
Quantifier: matches 2 or more of the preceding token (letter class) | com, io, org |
$ |
Anchor: asserts the position at the end of the string | End of 'user@example.com' |
Matches
user@example.comuser.name@sub.domain.couser-name@domain.ioa@b.cd123@456.xyz
Does not match
user@.com@example.comuser@example..comuser@example.cuser@example.com.user@-example.comuser@example-.com
Common pitfalls
- Does not allow quoted strings in the local part (e.g., "user name"@example.com)
- Does not enforce a maximum length for the local or domain parts
- Does not validate internationalized domain names (IDNs)
- Allows consecutive dots in the domain part (e.g., user@ex..ample.com)
- Does not restrict the top-level domain to common values (e.g., .com, .org)
- Allows hyphens at the start or end of domain labels (e.g., user@-example.com)
Performance notes
The pattern is linear in complexity and does not pose a risk of catastrophic backtracking. However, excessively long strings may still impact performance due to the unbounded quantifiers (+). Anchoring at both ends ensures the entire string is validated, preventing partial matches.