0

Possible Duplicate:
Validating an email Address in C#

Hi,

I'm trying to create regular expression to check valid email address, but the following code not returning any output.

foreach(Match m in (Regex.Matches("Jack@yahoo.com","^([a-zA-Z])+(?:\\d)*?@\\1\\.\\1{2,4}$")))
    {   
        Console.WriteLine("{0} found at index{1}",m.Value,m.Index);
    }

Could anyone please tell what's wrong I'm doing?

Community
  • 1
  • 1
Dusk
  • 2,191
  • 6
  • 38
  • 57
  • 2
    Don't make your regex too complicated, or better don't use a regex at all. See here for the reasons: [What is the best regular expression for validating email addresses?](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) – Dirk Vollmar Dec 08 '10 at 10:16
  • Your expression will reject lots of valid mail addresses. In particular TLDs can be much longer than four letters, the domain and there are many valid characters you missed. And regex aren't a good fit for validating email addresses. – CodesInChaos Dec 08 '10 at 10:24
  • http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html – LukeH Dec 08 '10 at 10:25

1 Answers1

0

Try this

    foreach(Match m in (Regex.Matches("Jack@yahoo.com","^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$")))
    {   
        Console.WriteLine("{0} found at index{1}", m.Value, m.Index);
    }
Mahdi jokar
  • 1,267
  • 6
  • 30
  • 50
pooja
  • 2,334
  • 3
  • 16
  • 16