Regex problem Email test
i have some problem with pattern bellow:
/([A-Z0-9]+[A-Z0-9._+-]*){3,64}@(([A-Z0-9]+([-][A-Z0-9])*){2,}.)+([A-Z0-9]+([-][A-Z0-9])*){2,}/i
It match email addresses and i have problem with this rule:
[A-Z0-9._+-]*
If i remove the star it works but i want this characters to be 0 or more. I tested it on http://regexpal.com/ and it works but on preg_match_all (PHP) - didn't work
Thanks
First of all, there are plenty of resources for this available. A quick search for "email validation regex" yields tons of results... Including This One...
Secondly, the problem is not in the *
character. The problem is in the whole block.
([A-Z0-9]+[A-Z0-9._+-]*){3,64}
Look at what that's doing. It's basically saying match as many alpha-numerics as possible, then match as many alpha-numerics with other characters as possible, then repeat at least 3 and at most 64 times. That could be a LOT of characters...
Instead, you could do:
([A-Z0-9][A-Z0-9._+-]{2,63})
Which will at most result in a match against a 64 character email.
Oh, and this is the pain of parsing emails with regex
There are plenty of other resources for validating email addresses (Including filter_var
). Do some searching and see how the popular frameworks do it...
Why not use PHPs filter_var()
filter_var('test@email.com', FILTER_VALIDATE_EMAIL)
There is no good regex to validate email addresses. If you absolutely MUST use regex, then maybe have a look at Validate an E-Mail Address with PHP, the Right Way. Although, this is by no means a perfect measure either.
Edit: After some digging, I came across Mailparse.
Mailparse is an extension for parsing and working with email messages. It can deal with » RFC 822 and » RFC 2045 (MIME) compliant messages.
Mailparse is stream based, which means that it does not keep in-memory copies of the files it processes - so it is very resource efficient when dealing with large messages.
Try this regex :
/^[A-Z0-9][A-Z0-9._+-]{3,64}@([A-Z0-9][-A-Z0-9]*.)+[A-Z0-9]{2,}$/i
But like @Russell Dias said, you shouldn't use regex for emails.
链接地址: http://www.djcxy.com/p/92732.html上一篇: 如何解决这个登录脚本的问题?
下一篇: 正则表达式问题电子邮件测试