1

I have code in console app

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);
}

when I debug it it shows that the coll is empty could you tell whats problem I am solving it about an hour

Ignacio Correia
  • 3,611
  • 8
  • 39
  • 68
user3150998
  • 37
  • 1
  • 8
  • You do know that your regular expression doesn't come near to covering all valid email addresses? – Enigmativity Jan 25 '14 at 09:00
  • 1
    This, for example, is a valid email address - `"very.unusual.@.unusual.com"@example.com` - and so is this - `!#$%&'*+-/=?^_{}|~@example.org` – Enigmativity Jan 25 '14 at 09:06

2 Answers2

5

try this

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
  }
}
santosh singh
  • 27,666
  • 26
  • 83
  • 129
1

Your regular expression worked for me if I take out / & /i.

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

alternatively, you can also use this...

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

Buddha
  • 4,339
  • 2
  • 27
  • 51