In daily development,Judgment emailIt is indispensable. I will use **C#** as an example to write a judgment method.Regular expressionsIt's universal, CV is fine
First introduce the namespace that regularity needs to be used
// Regular verification referenceusing ;
Determine whether it is a QQ email
/// <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,}@$"; Regex mReg = new Regex(str); if ((mail)) { return true; } return false; }
Here is a regular method to determine whether it is a QQ mailbox method. The regular expression is below
^[1-9][0-9]{4,}@$
Determine whether it is an email address
Here we first understand the commonly used email domain name suffix. Currently, except for many personal corporate emails and domain mailboxes, basically the normal email addresses are com and net domain names.
Therefore, our regular expression is directly limited to@**.com
Ending or@**.net
Ending.
/// <summary> /// Verify that it is an email/// </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 ((mail)) { return true; } return false; }
The following 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, which means that personal email addresses with domain names com, cn and net are allowed to match.
Supplement: C# regular expressions (Regex class)
When using regular expressions in C# language, the Regex class should be used, which is in the namespace.
The Regex class represents the .NET Framework regular expression engine. It can be used to quickly analyze large amounts of text to find specific character patterns; to extract, edit, replace, or delete text substrings; and to add the extracted string to the collection to generate reports.
Use the IsMatch method in the Regex class to determine whether the matching string meets the requirements of regular expressions.
[Example] Enter a mailbox from the console in the Main method and use regular expressions to determine its correctness.
According to the requirements of the question, in this example, the regular expression for mailbox verification is written with @, before @ is a letter or number, and a line is drawn below, after @ is a letter or number, and a line is drawn below, and the letter or number must be added after ., and the letter or number must be added after . The specific code is as follows.
class Program { static void Main(string[] args) { ("Please enter an email"); string email = (); Regex regex = new Regex(@"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$"); if ((email)) { ("The mailbox is in the correct format."); } else { ("The email format is incorrect."); } } }
Summarize
This is the end of this article about how to use regular expressions to judge email. For more related C# regular expressions to judge email, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!