我用这个
@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"
Regexp来验证电子邮件
([\w\.\-]+) -用于第一级域名(许多字母和数字,也有点和连字符) ([\w\-]+) -用于二级域 ((\.(\w){2,3})+) -这是用于其他级别域(从3到无穷),其中包括一个点和2或3个字面量
这个正则表达式有什么问题?
编辑:它与“something@someth.ing”电子邮件不匹配
我用这个
@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"
Regexp来验证电子邮件
([\w\.\-]+) -用于第一级域名(许多字母和数字,也有点和连字符) ([\w\-]+) -用于二级域 ((\.(\w){2,3})+) -这是用于其他级别域(从3到无穷),其中包括一个点和2或3个字面量
这个正则表达式有什么问题?
编辑:它与“something@someth.ing”电子邮件不匹配
当前回答
以下是我从这里收集信息和微软文档后的解决方案:
/// <summary>
/// * TLD support from 2 to 5 chars (modify the values as you want)
/// * Supports: abc@gmail.com.us
/// * Non-sensitive case
/// * Stops operation if takes longer than 250ms and throw a detailed exception
/// </summary>
/// <param name="email"></param>
/// <returns>valid: true | invalid: false </returns>
/// <exception cref="ArgumentException"></exception>
private bool validateEmailPattern(string email) {
try {
return Regex.IsMatch(email,
@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,5})+)$",
RegexOptions.None, TimeSpan.FromMilliseconds(250));
} catch (RegexMatchTimeoutException) {
// throw an exception explaining the task was failed
_ = email ?? throw new ArgumentException("email, Timeout/failed regexr processing.", nameof(email));
}
}
其他回答
作为Alex的流行答案的更新:在。net 5中,MailAddress现在有一个TryCreate。所以你可以这样做:
public static bool IsValidEmail(string email)
{
if (!MailAddress.TryCreate(email, out var mailAddress))
return false;
// And if you want to be more strict:
var hostParts = mailAddress.Host.Split('.');
if (hostParts.Length == 1)
return false; // No dot.
if (hostParts.Any(p => p == string.Empty))
return false; // Double dot.
if (hostParts[^1].Length < 2)
return false; // TLD only one letter.
if (mailAddress.User.Contains(' '))
return false;
if (mailAddress.User.Split('.').Any(p => p == string.Empty))
return false; // Double dot or dot at end of user part.
return true;
}
new System.ComponentModel.DataAnnotations.EmailAddressAttribute().IsValid(input)
这并不满足rfc 5321和5322的所有要求,但它适用于以下定义。
@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+"@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,17})$";
下面是代码
const String pattern =
@"^([0-9a-zA-Z]" + //Start with a digit or alphabetical
@"([\+\-_\.][0-9a-zA-Z]+)*" + // No continuous or ending +-_. chars in email
@")+" +
@"@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,17})$";
var validEmails = new[] {
"ma@hostname.com",
"ma@hostname.comcom",
"MA@hostname.coMCom",
"m.a@hostname.co",
"m_a1a@hostname.com",
"ma-a@hostname.com",
"ma-a@hostname.com.edu",
"ma-a.aa@hostname.com.edu",
"ma.h.saraf.onemore@hostname.com.edu",
"ma12@hostname.com",
"12@hostname.com",
};
var invalidEmails = new[] {
"Abc.example.com", // No `@`
"A@b@c@example.com", // multiple `@`
"ma...ma@jjf.co", // continuous multiple dots in name
"ma@jjf.c", // only 1 char in extension
"ma@jjf..com", // continuous multiple dots in domain
"ma@@jjf.com", // continuous multiple `@`
"@majjf.com", // nothing before `@`
"ma.@jjf.com", // nothing after `.`
"ma_@jjf.com", // nothing after `_`
"ma_@jjf", // no domain extension
"ma_@jjf.", // nothing after `_` and .
"ma@jjf.", // nothing after `.`
};
foreach (var str in validEmails)
{
Console.WriteLine("{0} - {1} ", str, Regex.IsMatch(str, pattern));
}
foreach (var str in invalidEmails)
{
Console.WriteLine("{0} - {1} ", str, Regex.IsMatch(str, pattern));
}
我在MSDN上找到了一个很好的文档。
如何:验证字符串是否在有效的电子邮件格式 http://msdn.microsoft.com/en-us/library/01escwtf.aspx (请注意,这段代码还支持在Internet域名中使用非ascii字符。)
有两个实现,用于。net 2.0/3.0和用于。net 3.5及更高版本。 2.0/3.0版本为:
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
我对这个方法的测试给出:
Invalid: @majjf.com
Invalid: A@b@c@example.com
Invalid: Abc.example.com
Valid: j..s@proseware.com
Valid: j.@server1.proseware.com
Invalid: js*@proseware.com
Invalid: js@proseware..com
Valid: ma...ma@jjf.co
Valid: ma.@jjf.com
Invalid: ma@@jjf.com
Invalid: ma@jjf.
Invalid: ma@jjf..com
Invalid: ma@jjf.c
Invalid: ma_@jjf
Invalid: ma_@jjf.
Valid: ma_@jjf.com
Invalid: -------
Valid: 12@hostname.com
Valid: d.j@server1.proseware.com
Valid: david.jones@proseware.com
Valid: j.s@server1.proseware.com
Invalid: j@proseware.com9
Valid: j_9@[129.126.118.1]
Valid: jones@ms1.proseware.com
Invalid: js#internal@proseware.com
Invalid: js@proseware.com9
Invalid: js@proseware.com9
Valid: m.a@hostname.co
Valid: m_a1a@hostname.com
Valid: ma.h.saraf.onemore@hostname.com.edu
Valid: ma@hostname.com
Invalid: ma@hostname.comcom
Invalid: MA@hostname.coMCom
Valid: ma12@hostname.com
Valid: ma-a.aa@hostname.com.edu
Valid: ma-a@hostname.com
Valid: ma-a@hostname.com.edu
Valid: ma-a@1hostname.com
Valid: ma.a@1hostname.com
Valid: ma@1hostname.com
下面是我们在这种情况下的正则表达式:
@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$",
有三个部分,这是检查。最后一个可能是你需要的。特定的术语{2,6}表示TLD最后的最小/最大长度。 HTH