0

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

  • *(recommended)* [Is there a PHP library for eMail Address validation](http://stackoverflow.com/questions/161342/is-there-a-php-library-for-email-address-validation) – Gordon Aug 25 '10 at 13:12
  • For reference, in a character class you don't have to do most of the escaping that you're doing. Instead of `[A-Z0-9\.\_\+\-]*` try `[-A-Z0-9._+]*`. – Jeff Rupert Aug 25 '10 at 13:14
  • 1
    *(fun)* [Mail::RFC822::Address: regexp-based address validation](http://ex-parrot.com/~pdw/Mail-RFC822-Address.html) – Gordon Aug 25 '10 at 13:15

5 Answers5

5

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.

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
  • [`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 Aug 25 '10 at 13:18
  • Ah. Thanks for sharing that Gordon. – Russell Dias Aug 25 '10 at 21:45
1

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
  • 163,128
  • 34
  • 264
  • 314
0

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
  • 91,525
  • 15
  • 160
  • 151
0

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____a___a___@gmail.com

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:

&^$@#&$^@#&aaa5a55a55a@gmail.comADA;'DROP TABLE :)
Piotr Müller
  • 5,323
  • 5
  • 55
  • 82
  • 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 Aug 25 '10 at 13:19
  • I've reedited samples, forgot of 0-9, no used _ to show what i mean – Piotr Müller Aug 25 '10 at 13:35
0

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 Sall-Storgaard
  • 10,576
  • 5
  • 36
  • 46