Use regular expressions to determine mailboxes

my blog

In daily development, judging mailboxes is indispensable. I take **C#** as an example to write a judgment method. Regular expressions are common, and CV can be used.

First introduce the namespace that regular needs to use

//正则验证引用
using System.Text.RegularExpressions;
复制代码

Determine whether it is a QQ mailbox

/// <summary>
///  验证QQ邮箱
/// </summary>
/// <param name="mail">邮箱</param>
/// <returns></returns>
public static bool CheckMail(string mail)
{
    string str = @"^[1-9][0-9]{4,}@qq.com$";
    Regex mReg = new Regex(str);

    if (mReg.IsMatch(mail))
    {
        return true;
    }
    return false;
}
复制代码

Here is the method of using regular to judge whether it is a QQ mailbox, the regular expression is below

^[1-9][0-9]{4,}@qq.com$
复制代码

Determine whether it is a mailbox

Here, let's take a look at the commonly used mailbox domain name suffixes. At present, in addition to many personal business mailboxes and domain name mailboxes, basically normal mailboxes are the same comas the netdomain name.

So our regular expression is directly limited to the @**.comend or the @**.netend.

/// <summary>
/// 验证是否为邮箱
/// </summary>
/// <param name="mail"></param>
/// <returns></returns>
public static bool CheckAllMail(string mail)
{
    string str = @"^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(com|cn|net)$";
    Regex mReg = new Regex(str);
 
    if (mReg.IsMatch(mail))
    {
        return true;
    }
    return false;
}
复制代码

Below is the regular expression

^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(com|cn|net)$
复制代码

The domain names I set here are com, cn and net, that is to say, personal mailboxes whose domain names are com, cn and net are allowed to match.

Guess you like

Origin juejin.im/post/7078545052414410759