My blog
Daily development, determine mailbox is indispensable, this I use **C#** as an example, to write a determination method, regular expression is universal, CV can be
We first introduce the namespace that the re needs to use
// Re validates references
using System.Text.RegularExpressions;
Copy the code
Check whether it is a QQ mailbox
/// <summary>
///Verify QQ mailbox
/// </summary>
/// <param name="mail">email</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;
}
Copy the code
Here is the method of using the regular expression to determine whether it is QQ mailbox, the regular expression is at the bottom
^ [19 -] [09 -] {4,}@qq.com$
Copy the code
Check whether it is a mailbox
Here we first understand the common mailbox domain name suffix, in addition to many personal enterprise mailbox and domain name mailbox, basically normal mailbox is COM and NET domain name.
So our regular expressions are directly limited to @**.com or @**.net ends.
/// <summary>
///Verify whether it is a mailbox
/// </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;
}
Copy the code
Here are the regular expressions
^[a-zA-Z09 -_.-]+@[a-zA-Z09 --]+(\.[a-zA-Z09 --]+)*\.(com|cn|net)$
Copy the code
Here I set the domain name com, cn and net, that is to say, allow the domain name com, CN and net personal email match oh.