tags:

views:

61

answers:

6

Hello, 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

+5  A: 

Why not use PHPs filter_var()

filter_var('[email protected]', 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.

Russell Dias
[`filter_var` up to but not including PHP 5.3.2 and 5.2.14 suffers from at least one bug though](http://stackoverflow.com/questions/3406473/why-does-filter-varemail-filter-validate-email-allow-testtest/3406651)
Gordon
Ah. Thanks for sharing that Gordon.
Russell Dias
A: 

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.

Colin Hebert
it is for extracting not for validating
Blood Drainer
Then you can add parenthesis to the regex, 3 pairs here.
Colin Hebert
A: 

You have quantity modifier after whole group:

([A-Z0-9]+[A-Z0-9\.\_\+\-]*){3,64}

So this will require minimum of 3 alphabetical characters and something like this:

a5__@gmail.com

will not work, but this:

a__aa[email protected]

will do the work. Better find a ready well tested regex.

Also, you don't have starting and ending delimiter, so something like this will pass:

&^$@#&$^@#&[email protected];'DROP TABLE :)
killer_PL
the quantity modifier check the whole group which can be aaa,555,a12,fg8,f7ysf87s,8dfs9df.... it's work for all emails the problem is in the star. If i remove it works, but with star it can't validate all emails
Blood Drainer
I've reedited samples, forgot of 0-9, no used _ to show what i mean
killer_PL
+1  A: 

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...

ircmaxell
A: 

While I agreed with Russel Dias, I believe your issue is with this entire block:

([A-Z0-9]+[A-Z0-9\.\_\+\-]*){3,64}

Basically you are saying, you want;

  • Letters or numbers, 1 or more times
  • Letters or numbers, 0 or more times
  • Repeat the above between 3 and 64 times
Kristoffer S Hansen
A: 

Thanks all, i've done it!

Blood Drainer