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