原始来源:
修改后加入了两个新功能:
1,匹配类似 "lc <deerchao@>" 这样的包含姓名的格式.
2,添加了四个命名组: name, email, user, domain. 这样可以方便取出相应的信息.
Regexre=newRegex(@"^((?'name'.+?)\s*<)?(?'email'(?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|""(?'user'(?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*""\x20*)*(?'angle'<))?(?'user'(?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|""((?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*"")@(?'domain'((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?'angle')(?(name)>)$",RegexOptions.Multiline|RegexOptions.ExplicitCapture);
MatchCollectionmc=re.Matches(@"lc<abc@>
Abc@
aBC@
abc.123@
");
foreach(Matchmainmc)
{
}
原表达式的最大优点就是匹配能力强大,能吃下各种符合规范(RFC2882)的表达式.
匹配WikiPedia上的10/11个合法邮件地址格式,不合法的一个也不匹配:
Valid e-mail addresses
abc@Abc@aBC@abc.123@1234567890@_______@abc+mailbox/department=shipping@!#$%&'*+-/=?^_`.{|}~@ (all of these characters are allowed)"abc@def"@ (anything goes inside quotation marks)"Fred Bloggs"@"%()[]\;:,<>"@
Invalid e-mail addresses
(character @ is missing)Abc.@ (character dot(.) is last in local part)Abc..123@ (character dot(.) is double)A@b@c@ (only one @ is allowed outside quotations marks)%()[]\;:,<>@ (none of the characters before the @ is allowed outside quotation marks)