使用正则表达式验证电子邮件地址时允许在“@”之前使用特殊字符
Allow special characters before '@' while validating email address with regex
在@之前允许使用特殊字符(即abc_@example.com)。我们使用以下正则表达式:
class Program
{
static bool IsValid(string value)
{
return Regex.IsMatch(value, "^(?(\")(\".+?\"@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
}
static void Main(string[] args)
{
Console.WriteLine(IsValid("example211_@hotmail.com"));
Console.ReadLine();
}
}
使用以下正则表达式来完全满足您的要求。
return Regex.IsMatch(value, @"^([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})(\]?)$");
在@之前允许使用特殊字符(即abc_@example.com)。我们使用以下正则表达式:
class Program
{
static bool IsValid(string value)
{
return Regex.IsMatch(value, "^(?(\")(\".+?\"@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
}
static void Main(string[] args)
{
Console.WriteLine(IsValid("example211_@hotmail.com"));
Console.ReadLine();
}
}
使用以下正则表达式来完全满足您的要求。
return Regex.IsMatch(value, @"^([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})(\]?)$");