Write ein robust email validation regex
Email validation regex ist notoriously komplex due in RFC 5322. Lernen practical Muster für gemein verwenden cases und ihre limitations.
- 1
Email validation is complex because RFC 5322 allows many valid formats (comments, quoted strings, IP addresses in brackets). A practical regex balances strictness and usability. For most applications, a moderate pattern is sufficient: ^[^\s@]+@[^\s@]+\.[^\s@]+$ checks basic structure (non-empty [email protected]).
- 2 Regex-Tester →
Test email patterns. Simple: ^[^\s@]+@[^\s@]+\.[^\s@]+$ catches obvious errors. Moderate: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ is more specific. Test against valid emails ([email protected], [email protected]) and invalid ones (missing @, spaces, no TLD).
- 3 Text-E-Mail-Extraktion →
Extrahieren email addresses from text. This tool finds all email-like patterns in a text block. Use it to validate that your regex finds the same emails — compare results to verify correctness.
Häufige Fragen
- Should I use regex to validate emails?
- For basic syntax checking, a simple regex is fine. For full RFC 5322 compliance, regex is impractical (the pattern is enormous). The best approach: use a simple regex for syntax, then send a verification email to confirm the address exists and is accessible. No regex can verify that an email is deliverable.
- What does the perfect email regex look like?
- There is no perfect email regex. RFC 5322 allows formats that no practical regex handles (e.g., "very.(),:;<>[]\".VERY.\"very@\ \"very\".unusual"@strange.example.com). The RFC 5322 regex is over 6KB. Use a library or a simple pattern + verification email instead.