使用RegEx c#从文本中提取电子邮件地址

时间:2014-01-25 08:40:37

标签: c# regex

我在控制台应用中有代码

reg = new Regex(@"/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i");
  string text = "wjeqklejqwek myEmail@hotmail.com a;lekqlwe anothermail@mail.ru";
  parseTextByTagName("", text);
  MatchCollection coll =   reg.Matches(text);
}

当我调试它时它表明coll是空的你能告诉我什么问题我解决它大约一个小时

2 个答案:

答案 0 :(得分:5)

试试这个

string strRegex = @"[A-Za-z0-9_\-\+]+@[A-Za-z0-9\-]+\.([A-Za-z]{2,3})(?:\.[a-z]{2})?";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"wjeqklejqwek myEmail@hotmail.com a;lekqlwe anothermail@mail.ru";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Add your code here
  }
}

答案 1 :(得分:1)

如果我拿出/& /i

[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?

或者,您也可以使用此...

/^[\w-\._\+%]+@(?:[\w-]+\.)+[\w]{2,6}$/