Write robust email validation regex
Email validation regex 是 notoriously 复杂由于 RFC 5322. Learn practical patterns 为常见用例和 其 limitations.
- 1
Email validation 是复杂 because RFC 5322 allows 许多有效 formats (comments, quoted strings, IP addresses 在 brackets). practical regex balances strictness 和 usability. 为最多 applications, moderate 模式是 充分: ^[^\s@]+@[^\s@]+\.[^\s@]+$ checks 基础结构 (non-empty 本地@域.tld).
- 2 正则表达式测试器 →
Test email patterns. 简单: ^[^\s@]+@[^\s@]+\.[^\s@]+$ catches 显然错误. Moderate: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ 是更多特定. Test 针对有效 emails (用户@示例.com, 第一.最后@sub.示例.co.uk) 和无效 ones (missing @, spaces, 否 TLD).
- 3 文本邮箱提取器 →
提取 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.
常见问题
- 应该 I use regex 到验证 emails?
- 为基础 syntax checking, 简单 regex 是 fine. 为完整 RFC 5322 合规, regex 是 impractical ( 模式是 enormous). best 方法: use 简单 regex 为 syntax, 然后发送验证 email 到 confirm address exists 和是 accessible. 否 regex 能够验证该 email 是 deliverable.
- 什么 perfect email regex look like?
- 那里是 否 perfect email regex. RFC 5322 allows formats 该否 practical regex handles (e.g., "very.(),:;<>[]\".VERY.\"very@\ \"very\".unusual"@strange.示例.com). RFC 5322 regex 是遍历 6KB. Use 库或 简单模式 + 验证 email instead.