正则表达式验证电子邮件

时间:2014-10-24 11:13:48

标签: c#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace read_test
{
    class StringCheck
    {
        static void Main(String [] args)
        {
            string email = "email@email.com";

        string regEx = @"\w+\.?\w+\@{1}\w+\.{1}\w+";

        Match aMatch;
        aMatch = Regex.Match(email, regEx);

        if (aMatch.Success)
            Console.WriteLine("Successfull.");
        else
            Console.WriteLine("Not Successfull");

        Console.Read();

    }
}

}

尝试编写正则表达式以检查输入的电子邮件ID是否有效。这是正确的吗?或者有更好的方式

1 个答案:

答案 0 :(得分:3)

试试这个,你真的不需要正则表达式

bool IsValidEmail(string email)
{
    try {
        var addr = new System.Net.Mail.MailAddress(email);
        return true;
    }
    catch {
        return false;
    }
}