正则表达式电子邮件验证 [英] Regex Email validation

查看:111
本文介绍了正则表达式电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个

@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"

regexp来验证电子邮件

regexp to validate the email

([\w\.\-] +)-适用于第一级域(许多字母和数字,也包括点和连字符)


([\w\-] +)-这是第二级域的


((\。()w){2,3})+ )-这适用于其他级别的域(从3到无穷大),其中包括一个点和2或3个文字

([\w\.\-]+) - this is for the first-level domain (many letters and numbers, also point and hyphen)

([\w\-]+) - this is for second-level domain

((\.(\w){2,3})+) - and this is for other level domains(from 3 to infinity) which includes a point and 2 or 3 literals

编辑:它与 something@someth.ing电子邮件不匹配

it doesn't match the "something@someth.ing" email

推荐答案

类似于 .museum 的TLD与之不符方式,还有其他一些长TLD。另外,您可以使用 MailAddress类来验证电子邮件地址a>就像Microsoft在笔记中此处所述:

TLD's like .museum aren't matched this way, and there are a few other long TLD's. Also, you can validate email addresses using the MailAddress class as Microsoft explains here in a note:


您可以使用System.Net.Mail.MailAddress类,而不使用正则表达式来验证电子邮件地址。要确定
电子邮件地址是否有效,请将电子邮件地址传递给
MailAddress.MailAddress(String)类构造函数。

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.



public bool IsValid(string emailaddress)
{
    try
    {
        MailAddress m = new MailAddress(emailaddress);

        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}

这样可以为您省去很多头痛的麻烦,因为您不会不必编写(或尝试了解别人的)正则表达式。

This saves you a lot af headaches because you don't have to write (or try to understand someone else's) regex.

这篇关于正则表达式电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆