我想搜索一个字符串的电子邮件地址,但我的正则表达式不起作用,当字符串包含除电子邮件以外的其他字符。意思是,如果我尝试像“[email protected]”这样的小字符串,则正则表达式会找到匹配项。如果我在字符串中插入空格,如:“[email protected]”,则正则表达式不会找到电子邮件匹配项。与正则表达式解析字符串
这里是我的代码(正则表达式模式是从网上):
string emailpattern = @"^(([^<>()[\]\\.,;:\[email protected]\""]+"
+ @"(\.[^<>()[\]\\.,;:\[email protected]\""]+)*)|(\"".+\""))@"
+ @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
+ @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
+ @"[a-zA-Z]{2,}))$";
Regex rEmail = new Regex(emailpattern);
string str = @" [email protected]";
MatchCollection mcolResults = rEmail.Matches(str);
MessageBox.Show(mcolResults.Count.ToString());
请让我知道我做错了。
谢谢。
此致
看看这里更好的电子邮件正则表达式:http://www.regular-expressions.info/email.html – jtbandes 2010-08-10 05:31:06