C# 验证电子邮件以不盲目接受电子邮件的最简单正则表达式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/742451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:25:08  来源:igfitidea点击:

What is the simplest regular expression to validate emails to not accept them blindly?

c#regexemail-validation

提问by eKek0

When users create an account on my site I want to make server validation for emails to not accept everyinput.

当用户在我的网站上创建帐户时,我想对电子邮件进行服务器验证以不接受所有输入。

I will send a confirmation, in a way to do a handshake validation.

我将发送确认,以某种方式进行握手验证

I am looking for something simple, not the best, but not too simple that doesn't validate anything. I don't know where limitation must be, since any regular expression will not do the correct validation because is not possible to do it with regular expressions.

我正在寻找一些简单东西,不是最好的,但也不是太简单的不能验证任何东西的东西。我不知道限制在哪里,因为任何正则表达式都不会进行正确的验证,因为使用正则表达式是不可能的。

I'm trying to limit the sintax and visual complexity inherent to regular expressions, because in this case any will be correct.

我试图限制正则表达式固有的语法和视觉复杂性,因为在这种情况下,任何都是正确的。

What regexp can I use to do that?

我可以使用什么正则表达式来做到这一点?

采纳答案by chaos

^\S+@\S+$

回答by Guffa

It's possible to write a regular expression that only accept email addresses that follow the standards. However, there are some email addresses out there that doesn't strictly follow the standards, but still work.

可以编写一个只接受符合标准的电子邮件地址的正则表达式。但是,有一些电子邮件地址没有严格遵守标准,但仍然有效。

Here are some simple regular expressions for basic validation:

以下是一些用于基本验证的简单正则表达式:

Contains a @ character:

包含 @ 字符:

@

Contains @ and a period somewhere after it:

包含 @ 和它后面的一个句点:

@.*?\.

Has at least one character before the @, before the period and after it:

在@ 之前、句点之前和之后至少有一个字符:

.+@.+\..+

Has only one @, at least one character before the @, before the period and after it:

只有一个@,在@ 之前,句点之前和之后至少有一个字符:

^[^@]+@[^@]+\.[^@]+$

User AmoebaMan17 suggests this modification to eliminate whitespace:

用户 AmoebaMan17 建议进行此修改以消除空格:

^[^@\s]+@[^@\s]+\.[^@\s]+$

And for accepting only one period:

并且只接受一个时期:

^[^@\s]+@[^@\s\.]+\.[^@\.\s]+$

回答by JP Alioto

Take your pick.

随你挑。

Here's the one that complies with RFC 2822 Section 3.4.1...

这是符合RFC 2822 Section 3.4.1...

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Just in case you are curious. :)

以防万一你好奇。:)

回答by coto

^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$

^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$

  • Only 1 @
  • Several domains and subdomains
  • 只有1个 @
  • 多个域和子域

回答by Carl Howarth

I think this little tweak to the expression by AmoebaMan17 should stop the address from starting/ending with a dot and also stop multiple dots next to each other. Trying not to make it complex again whilst eliminating a common issue.

我认为 AmoebaMan17 对表达式的这个小调整应该可以阻止地址以点开头/结尾,并且还可以阻止多个点相邻。在消除一个常见问题的同时,尽量不让它再次变得复杂。

(?!.*\.\.)(^[^\.][^@\s]+@[^@\s]+\.[^@\s\.]+$)

It appears to be working (but I am no RegEx-pert). Fixes my issue with users copy&pasting email addresses from the end of sentences that terminate with a period.

它似乎正在工作(但我不是 RegEx-pert)。解决了用户从句号结尾处复制和粘贴电子邮件地址的问题。

i.e: Here's my new email address [email protected].

即:这是我的新电子邮件地址 [email protected]