Possible Duplicates:
Email Validation - Regular Expression
What is the best regular expression for validating email addresses?
Hi All,
I have an email address roughly like this,
Which doesn't work with the regex I have here for email addresses. It doesn't seem to like the 4 at the start of the domain.
private const string MatchEmailPattern =
@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" +
@"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\." +
@"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" +
@"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
Most other corner cases work well with this regex, all of the below are rejected,
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("[email protected]"));
Any other regexes people can suggest for emails that will work with the above?
Also the above regex has the advantage that it works with addresses like this, and a lot of them don't,