How to make regex work on corner case email address
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,
firstname.lastname@4domain.co.nz
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("..@test.com"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress(".a@test.com"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress(".@s.dd"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("ab@988.120.150.10"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("ab@120.256.256.120"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("2@bde.cc"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("-@bde.cc"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("..@bde.cc"));
Assert.IsFalse(EmailValidator.IsValidEmailAddress("_@bde.cc"));
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,
firstname.lastname_@gmail.com
您应该使用MailAddress
类,如下所示:
try {
address = new MailAddress(address).Address;
} catch(FormatException) {
//address is invalid
}
Honestly? I might be unpopular for saying this, but why not just match
.+@.+
Minimalist but functional for 90% of cases.
A fun fact is that (unlike in most languages), it is possible to write a 'regex' in C#/.NET which fully matches the RFC5322 spec for email addresses. Here is one I prepared earlier (link shows the construction):
^(?'localPart'((((((((?'paren'()|(?'-paren'))|([u0021-u0027u002a
-u005bu005d-u007e]|[u0001-u0008u000bu000cu000e-u001fu007f])|
([ t]+((rn)[ t]+)?|((rn)[ t]+)+)|([u0021-u007e]|[ t]|[rn
]|[u0001-u0008u000bu000cu000e-u001fu007f]))*(?(paren)(?!))))
)|([ t]+((rn)[ t]+)?|((rn)[ t]+)+))*?(([a-zA-Z0-9!#$%&'*+/=?^_`
{|}~-]+)|("(([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?(([u0021u0023-u
005bu005d-u007e]|[u0001-u0008u000bu000cu000e-u001fu007f])|(
[u0021-u007e]|[ t]|[rn ]|[u0001-u0008u000bu000cu000e-u001f
u007f])))*([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?"))((((((?'paren'
()|(?'-paren'))|([u0021-u0027u002a-u005bu005d-u007e]|[u0001-u
0008u000bu000cu000e-u001fu007f])|([ t]+((rn)[ t]+)?|((rn)[
t]+)+)|([u0021-u007e]|[ t]|[rn ]|[u0001-u0008u000bu000cu
000e-u001fu007f]))*(?(paren)(?!)))))|([ t]+((rn)[ t]+)?|((rn)
[ t]+)+))*?)(.(((((((?'paren'()|(?'-paren'))|([u0021-u0027u002
a-u005bu005d-u007e]|[u0001-u0008u000bu000cu000e-u001fu007f])
|([ t]+((rn)[ t]+)?|((rn)[ t]+)+)|([u0021-u007e]|[ t]|[r
n ]|[u0001-u0008u000bu000cu000e-u001fu007f]))*(?(paren)(?!)))
))|([ t]+((rn)[ t]+)?|((rn)[ t]+)+))*?(([a-zA-Z0-9!#$%&'*+/=?^_
`{|}~-]+)|("(([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?(([u0021u0023-
u005bu005d-u007e]|[u0001-u0008u000bu000cu000e-u001fu007f])|
([u0021-u007e]|[ t]|[rn ]|[u0001-u0008u000bu000cu000e-u001
fu007f])))*([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?"))((((((?'paren'
()|(?'-paren'))|([u0021-u0027u002a-u005bu005d-u007e]|[u0001-
u0008u000bu000cu000e-u001fu007f])|([ t]+((rn)[ t]+)?|((rn)[
t]+)+)|([u0021-u007e]|[ t]|[rn ]|[u0001-u0008u000bu000cu
000e-u001fu007f]))*(?(paren)(?!)))))|([ t]+((rn)[ t]+)?|((rn)
[ t]+)+))*?))*))@(?'domain'((((((((?'paren'()|(?'-paren'))|([u002
1-u0027u002a-u005bu005d-u007e]|[u0001-u0008u000bu000cu000e-
u001fu007f])|([ t]+((rn)[ t]+)?|((rn)[ t]+)+)|([u0021-u007
e]|[ t]|[rn ]|[u0001-u0008u000bu000cu000e-u001fu007f]))*(?(
paren)(?!)))))|([ t]+((rn)[ t]+)?|((rn)[ t]+)+))*?(([a-zA-Z0-9
!#$%&'*+/=?^_`{|}~-]+)|("(([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?(([
u0021u0023-u005bu005d-u007e]|[u0001-u0008u000bu000cu000e-u00
1fu007f])|([u0021-u007e]|[ t]|[rn ]|[u0001-u0008u000bu000
cu000e-u001fu007f])))*([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?"))((
((((?'paren'()|(?'-paren'))|([u0021-u0027u002a-u005bu005d-u00
7e]|[u0001-u0008u000bu000cu000e-u001fu007f])|([ t]+((rn)[ t
]+)?|((rn)[ t]+)+)|([u0021-u007e]|[ t]|[rn ]|[u0001-u0008
u000bu000cu000e-u001fu007f]))*(?(paren)(?!)))))|([ t]+((rn)[
t]+)?|((rn)[ t]+)+))*?)(.(((((((?'paren'()|(?'-paren'))|([u00
21-u0027u002a-u005bu005d-u007e]|[u0001-u0008u000bu000cu000e-
u001fu007f])|([ t]+((rn)[ t]+)?|((rn)[ t]+)+)|([u0021-u00
7e]|[ t]|[rn ]|[u0001-u0008u000bu000cu000e-u001fu007f]))*(?
(paren)(?!)))))|([ t]+((rn)[ t]+)?|((rn)[t]+)+))*?(([a-zA-Z0-9
!#$%&'*+/=?^_`{|}~-]+)|("(([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?(([
u0021u0023-u005bu005d-u007e]|[u0001-u0008u000bu000cu000e-u00
1fu007f])|([u0021-u007e]|[ t]|[rn ]|[u0001-u0008u000bu000
cu000e-u001fu007f])))*([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?"))((
((((?'paren'()|(?'-paren'))|([u0021-u0027u002a-u005bu005d-u00
7e]|[u0001-u0008u000bu000cu000e-u001fu007f])|([ t]+((rn)[ t
]+)?|((rn)[ t]+)+)|([u0021-u007e]|[ t]|[rn ]|[u0001-u0008
u000bu000cu000e-u001fu007f]))*(?(paren)(?!)))))|([ t]+((rn)[
t]+)?|((rn)[ t]+)+))*?))*)|(((((((?'paren'()|(?'-paren'))|([u0
021-u0027u002a-u005bu005d-u007e]|[u0001-u0008u000bu000cu000e
-u001fu007f])|([ t]+((rn)[ t]+)?|((rn)[ t]+)+)|([u0021-u0
07e]|[ t]|[rn ]|[u0001-u0008u000bu000cu000e-u001fu007f]))*(
?(paren)(?!)))))|([ t]+((rn)[ t]+)?|((rn)[ t]+)+))*?[(([ t]+
((rn)[ t]+)?|((rn)[ t]+)+)?([!-Z^-~]|[u0001-u0008u000bu000c
u000e-u001fu007f]))*([ t]+((rn)[ t]+)?|((rn)[ t]+)+)?](((((
(?'paren'()|(?'-paren'))|([u0021-u0027u002a-u005bu005d-u007e]|
[u0001-u0008u000bu000cu000e-u001fu007f])|([ t]+((rn)[ t]+)?
|((rn)[ t]+)+)|([u0021-u007e]|[ t]|[rn ]|[u0001-u0008u00
0bu000cu000e-u001fu007f]))*(?(paren)(?!)))))|([ t]+((rn)[ t]+
)?|((rn)[ t]+)+))*?))$
However, note that RFC5322's handling of domain names is more liberal than the actual domain name RFCs, and there are also additional restrictions which apply from various RFCs (eg SMTP enforces a maximum length). So things which RFC5322 considers email addresses can still be invalid by other measures.
The acid test is still just: send an email to it with a verification code.
链接地址: http://www.djcxy.com/p/2702.html上一篇: PHP的过滤器