简体   繁体   English

正则表达式验证国际电子邮件

[英]Regex to validate International email

Not sure if there is a solution available but not able to find. 不确定是否有可用的解决方案但无法找到。 So asking it again. 所以再问一遍。

I am writing an email validator. 我正在写一封电子邮件验证员。 This should validate all well formed email(This is just one level of validation to check the email is well formed). 这应该验证所有格式良好的电子邮件(这只是验证电子邮件格式正确的一个级别的验证)。 Now as my code is an international code so i should support non latin characters also. 现在我的代码是国际代码,所以我也应该支持非拉丁字符。 How do i write the most efficient Regex for that? 我如何为此编写最有效的正则表达式?

International Emails: http://en.wikipedia.org/wiki/International_email 国际电子邮件: http//en.wikipedia.org/wiki/International_email

Some example emails: 一些示例电子邮件:

  1. 伊昭傑@郵件.商務 伊昭杰@邮件。商务
  2. राम@मोहन.ईन्फो राम@मोहन.ईन्फो
  3. юзер@екзампл.ком юзер@екзампл.ком
  4. θσερ@εχαμπλε.ψομ θσερ@εχαμπλε.ψομ

It should be able to validate all the above formats 它应该能够验证所有上述格式

The reason why email validation through regexp is so lax is because it's not efficient. 通过regexp进行电子邮件验证的原因是如此宽松是因为它效率不高。 There is a spec for email address syntax, but the regexp to check it is so long, it's impractical . 电子邮件地址的语法规范,但正则表达式来检查它这么久,这是不切实际的 Besides, email providers are more strict in their implementation of the syntax than the actual spec. 此外,电子邮件提供商在实现语法方面比实际规范更严格。 An email might be considered valid as the spec says it is, but invalid according to the provider. 根据规范说明,电子邮件可能被视为有效,但根据提供商而言无效。

That's also the reason why activation emails exist, because the only way to check if an email is valid, existing and currently in use is to send it something, usually a unique activation code or link. 这也是存在激活电子邮件的原因,因为检查电子邮件是否有效,现有和当前正在使用的唯一方法是向其发送一些内容,通常是唯一的激活码或链接。 Only when that unique activation code or link, only sent to that email, is used will the email be considered valid. 仅当使用仅发送到该电子邮件的唯一激活码或链接时,才将该电子邮件视为有效。

Until then, consider a more lax approach in validating emails, checking if the username, the @ and the domain parts. 在此之前,请考虑一种更加宽松的方法来验证电子邮件,检查用户名, @和域名部分。 Besides, why would one sign up using a false email anyway? 此外,为什么一个人会使用虚假电子邮件注册? If they did, they wouldn't get the activation link, and can't proceed creating the account. 如果他们这样做,他们将无法获得激活链接,并且无法继续创建帐户。

@Patashu Thanks a lot. @Patashu非常感谢。 I've improved your regex a bit and now it's absolutely suits my needs: 我已经改进了你的正则表达式,现在它非常适合我的需求:

^([^@\s\."'\(\)\[\]\{\}\\/,:;]+\.)*[^@\s\."'\(\)\[\]\{\}\\/,:;]+@[^@\s\."'\(\)\[\]\{\}\\/,:;]+(\.[^@\s\."'\(\)\[\]\{\}\\/,:;]+)+$

In case of Java, this one works quite well for me. 对于Java,这个对我来说非常好用。

"^[\\p{L}\\p{N}\\._%+-]+@[\\p{L}\\p{N}\\.\\-]+\\.[\\p{L}]{2,}$"

It does not allow IP's after the @ but most valid email in the from of xxx@xxx.TDL could be validated with it. 它不允许在@之后使用IP,但是xxx@xxx.TDL中的大多数有效电子邮件都可以通过它进行验证。 \\p{L} validates UTF-Letters and \\p{N} validates UTF-Numbers. \\p{L}验证UTF-Letters, \\p{N}验证UTF-Numbers。 You can check this doc for more information. 您可以查看此文档以获取更多信息。

This works for me using Python if you do not have special letters,characters but have dashes(-), numbers, both in the domain and in the username. 如果您没有特殊字母,字符但在域和用户名中都有破折号( - ),数字,这对我来说很有用。 This also matches if you have country extensions. 如果您有国家/地区扩展名,也会匹配。

[a-zA-Z0-9.-]+@[a-zA-Z-]+.(com|edu|net)(.([az]+))* [A-ZA-Z0-9 .-] + @ [A-ZA-Z - ] +。(([AZ] +))(COM | | edu的净)*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM