Do you mean underscores as in: _ ? Hyphens are -, well technically my writer wife tells me that there is difference between a dash - and a hyphen - but I'll be damned if I learn what it is.
–
Meep3DJan 14 '10 at 16:06

Yes, I meant underscore. It is too early in the morning for me to be working with regexes. :) Thanks for catching my mistake!
–
SesameJan 14 '10 at 16:07

The difference between hyphens, dashes, minusses, etc pp is annoying enough for them to form their own unicode character class: "Punctuation Dash"
–
gha.stJan 14 '10 at 17:21

5 Answers
5

If it is okay to start an email address with an underscore, add _ to both of the character classes that appear before @

^[-a-zA-Z0-9_][-.a-zA-Z0-9_]*@...

If the email id cannot start with an _, add it only to the second character class:

^[-a-zA-Z0-9][-.a-zA-Z0-9_]*@...

That said, your regex has a couple of issues:

It accepts email addresses starting with a hyphen; is this intended? If not, remove the - from the first character class to make it [a-zA-Z0-9]

It accepts consecutive periods after the first character thereby making 3...@example.com a valid id - is this status-by-design?

RFC specification for email address is quite complicated. See thesethreads for more information. Also don't forget to check the one and only perfect and the official regex for validating email addresses (be warned that you might find it a little longer than what sanity would suggest)

Regular-expressions.info has a very good discussion of e-mail address validation by regex, including his preferred regex for "99% of all e-mail addresses in use today", and another to match e-mail addresses as defined by RFC-2822.

I won't do the author a disservice by copying his work here. But I do think it's worthy of a read since it's directly related to your question.

The key thing to note in this grammar is that the local-part is almost free-form when it comes to the local part. And there are characters allowed in the local part like !, *, $, etc that are totally legal according to RFC2822 that aren't allowed.

and ...

Adi Oltean pointed out that V2 of the .Net framework contains the System.Net.MailAddress class which contains a built-in validator.

It looks like the System.Net.Mail.MailAddress constructor validates email addresses and you can catch a FormatException to ensure that the email is valid.