Regular Expression application: matching the email address

Source: Internet
Author: User

Email consists of three main parts: 1 user name part 2 @ 3 domain name part 1 user name part user name generally consists of a numerical letter underline, so the regular expression is: [\ da-zA-Z _] + can also be written as [0-9a-zA-Z _] + 2 @ because it is a fixed common character, the regular expression is: @ 3 the domain name is generally a combination of several numbers and letters followed by the DoT number. It will be repeated once or multiple times, and finally the top-level domain name (including the national domain name and organization) the regular expression is [-\ dA-Za-z] + \.) + [a-zA-Z] {2,} ([-\ dA-Za-z] + \.) +; match the combination of letters and numbers, followed by a dot, repeated once or multiple times [a-zA-Z] {2,}: match top-level domain name, consisting of letters, therefore, the regular expression matching the email address must contain at least two characters: [\ da-zA-Z _] + @ ([-\ dA-Za-z] + \.) + [a-zA-Z] {2,} test code:

Private static void Test () {List <string> emailList = new List <string> (); string email = "xue @ 163 ., xue@163.com12, 2707@qq.com, xue@yahoo.com.cn, xue@163.com, xue@163.com12 "; // Regex reg2 = new Regex (@ "^ \ da-zA-Z _] + @ ([-\ dA-Za-z] + \.) + [a-zA-Z] {2,} $ "); Verify the regular expression Regex reg = new Regex (@"(? <Email> [\ da-zA-Z _] + @ ([-\ dA-Za-z] + \.) + [a-zA-Z] {2,}) "); Match m = reg. match (email); foreach (Match item in reg. matches (email) {emailList. add (item. groups ["email"]. value );}}

 

Because there is no limit on the length, it will still match some wrong mailbox, such as xue@163.coommmmmmmmm, if you need more accurate match also need to further modify the domain name top-level domain name

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.