简体   繁体   English

电子邮件输入正则表达式验证

[英]Email entry regex validation

I am using the following regex to validate an email address: 我正在使用以下正则表达式来验证电子邮件地址:

"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|[a-zA-Z]{2})$"

Unfortunately, this does not allow email addresses with hyphens underscores. 不幸的是,这不允许带有 连字符 下划线的电子邮件地址。 Ex.: 例:

first_last@abc.com first_last@abc.com

How can I modify this to allow hyphens underscores? 我如何修改它以允许 连字符 下划线?

_ is not hyphen, it is underscore. _不是连字符,而是下划线。 Hyphen is - 连字符为-

If it is okay to start an email address with an underscore, add _ to both of the character classes that appear before @ 如果可以用下划线开头的电子邮件地址,请将_添加到出现在@之前的两个字符类中

^[-a-zA-Z0-9_][-.a-zA-Z0-9_]*@...

If the email id cannot start with an _ , add it only to the second character class: 如果电子邮件ID不能以_开头,则仅将其添加到第二个字符类:

^[-a-zA-Z0-9][-.a-zA-Z0-9_]*@...

That said, your regex has a couple of issues: 也就是说,您的正则表达式有两个问题:

  1. It accepts email addresses starting with a hyphen; 它接受以连字符开头的电子邮件地址; is this intended? 这是打算吗? If not, remove the - from the first character class to make it [a-zA-Z0-9] 如果不是,请从第一个字符类中删除-以使其成为[a-zA-Z0-9]
  2. It accepts consecutive periods after the first character thereby making 3...@example.com a valid id - is this status-by-design ? 它在第一个字符之后接受连续的时间段,从而使3...@example.com为有效ID-这是设计状态吗?

RFC specification for email address is quite complicated. 电子邮件地址的RFC规范非常复杂。 See these threads for more information. 有关更多信息,请参见这些 线程 Also don't forget to check the one and only perfect and the official regex for validating email addresses (be warned that you might find it a little longer than what sanity would suggest) 另外不要忘记检查独一无二的完善,官方正则表达式 ,用于验证的电子邮件地址(被警告,你可能会发现它比理智建议稍长

"^[-_a-zA-Z0-9][-_.a-zA-Z0-9]*@[-_.a-zA-Z0-9]+(\.[_-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|[a-zA-Z]{2})$"

可能?

^[-a-zA-Z0-9_][-.a-zA-Z0-9_]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|[a-zA-Z]{2})$

我在您的两个字符类中添加了“ _”。

Regular-expressions.info has a very good discussion of e-mail address validation by regex , including his preferred regex for "99% of all e-mail addresses in use today", and another to match e-mail addresses as defined by RFC-2822. Regular-expressions.info对regex进行的电子邮件地址验证进行了很好的讨论,包括他偏爱的regex用于“当今使用的所有电子邮件地址的99%”,以及另一个与RFC定义的电子邮件地址匹配的正则表达式-2822。

I won't do the author a disservice by copying his work here. 我不会在这里复制他的作品而给作者带来损害。 But I do think it's worthy of a read since it's directly related to your question. 但我确实值得一读,因为它与您的问题直接相关。

There is also an interesting blog post about email validation on Larry Osterman's website. 拉里·奥斯特曼(Larry Osterman)的网站上还有一篇有趣的博客文章,涉及电子邮件验证

This is a followup post to the original post in which he attempts to generate a regular expression to validate an email address. 这是原始帖子的后续帖子,在该帖子中,他尝试生成正则表达式来验证电子邮件地址。 His RegExp is: 他的RegExp是:

string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + 
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

His notes: 他的笔记:

The key thing to note in this grammar is that the local-part is almost free-form when it comes to the local part. 在此语法中要注意的关键是,本地部分几乎是自由形式。 And there are characters allowed in the local part like !, *, $, etc that are totally legal according to RFC2822 that aren't allowed. 并且在本地部分中允许使用的字符,如!,*,$等,根据RFC2822,这些字符完全合法,这是不允许的。

and ... 还有...

Adi Oltean pointed out that V2 of the .Net framework contains the System.Net.MailAddress class which contains a built-in validator. Adi Oltean指出,.Net框架的V2包含System.Net.MailAddress类,该类包含内置验证器。

It looks like the System.Net.Mail.MailAddress constructor validates email addresses and you can catch a FormatException to ensure that the email is valid. 看起来System.Net.Mail.MailAddress构造函数将验证电子邮件地址,并且您可以捕获FormatException以确保电子邮件有效。

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

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