c# - 如何使用正则表达式处理长电子邮件地址?

标签 c# regex

<分区>

我有以下用于电子邮件验证的正则表达式:

private const string ValidEmailRegexPattern = @"^(?:[^@\s\\(),:;<>[\]""]+|(?:(?:^|\.)""(?:[^\r\\"";]|(?:\\[\\""]))*"")+)+(?<=^.{1,64})@[^\s~!@#$%^&*()=+_{}\|;,`'""?<>]{1,256}$";

public static bool IsValidEmail(string email)
{
     return !string.IsNullOrWhiteSpace(email) && ValidEmailRegex.IsMatch(email);
}

但是当输入是有效的电子邮件时它会被卡住,但具有最大有效长度(254 个符号),如下所示:

"123...@gmail.com" - 254 符号,包括 244 数字和 @gmail.com

如何更改我的正则表达式?我希望我的程序可以处理那种类型的电子邮件地址。

最佳答案

您可以使用 MailAddress类来验证电子邮件而不是使用正则表达式验证它。

MailAddress m = new MailAddress(email);

来自 MSDN

Instead of using a regular expression to validate an email address, you can use the System.Net.Mail.MailAddress class. To determine whether an email address is valid, pass the email address to the MailAddress.MailAddress(String) class constructor.

关于c# - 如何使用正则表达式处理长电子邮件地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33672635/

相关文章:

c# - 带有 await 的异步 lambda 表达式返回任务?

c# - "nested if"与使用 F# 的 "if and"性能

c# - 简单 ASP.NET Web API 中的依赖注入(inject)

c# - 在 Moq 中为返回 void 的方法分配参数

regex - 如何在正则表达式中指定可选字符?

regex - 重命名文件夹及其子文件夹的文件

MySQL 的 c# 包装器类,在方法之间共享 "MySqlConnection()"对象是个好主意吗?

javascript - js regex - 获取表达式之间的字符串

javascript - 从全名字段中解析称呼和名字

C SLRE 正则表达式库不起作用