0

I have to following code to validate an email address

 var reg = new Regex(@"/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/");

 string e1 = "name@host.net";
 string e2 = "namehost.net";

 bool b1 = reg.IsMatch(e1);
 bool b2 = reg.IsMatch(e2);

but both b1 and b2 fail

exexzian
  • 7,782
  • 6
  • 41
  • 52
Jason94
  • 13,320
  • 37
  • 106
  • 184
  • 2
    You're close but the actual regex is: http://ex-parrot.com/~pdw/Mail-RFC822-Address.html – aquinas Feb 09 '13 at 19:45
  • 2
    Actually, the actual regex is here: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address/1771483#1771483. RFC822 was obsoleted almost 12 years ago. – Michael Feb 09 '13 at 20:10

1 Answers1

3

Remove the slashes at the beginning and end.

var reg = new Regex(@"^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$");

However, that being said, your regex is not a good pattern for matching e-mail addresses. In fact, an accurate pattern is really difficult to make. Google some and you should find better ones.

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • @Joey, really worse? This one accepts plenty of dots (`.@....` is matched as valid), non-latin digits in the host name (such as arabic ones like ۰۱۲۳۴۵۶۷۸۹), and in revenge doesn't accept all characters it should otherwise. Not to say that it doesn't support the direct IP address format and the like. – Lucero Feb 10 '13 at 02:28