0

I'm currently using this regex in my php script to validate email addresses:

preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);

Problem is, this specific regex is not reliable and often finds some false positives.

I was looking at regexlib.com to find a better one, but none of the ones I get from that site seem to find matches.

for example:

 preg_match_all("^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$", $string, $matches);

finds nothing, even though it should work according to regexlib. I've tried the top 10 regex terms on regexlib for email validation and none of them work. Am I doing something wrong?

Link to regexlib email category: http://regexlib.com/DisplayPatterns.aspx?cattabindex=0&categoryId=1

Thanks,

Or Weinberger
  • 7,332
  • 23
  • 71
  • 116

2 Answers2

3

Your second regex fails because you need to add delimiters in PHP:

"/([0-9a-zA-Z]...<snip>...[a-zA-Z]{2,9})/"
 ^                                      ^
 here                                here

(I also removed the ^ and $ as Mario pointed out.)

You could try a different approach though:

  1. Use a very simple regular expression to find anything that looks like it might be an e-mail address. It's OK to allow false-positives here, just make sure you don't miss any.
  2. For each email address candidate you found above, check it against a proper e-mail address validator to see if it is actually a valid e-mail address. Use this stage to remove the false positives.

Related

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

Three things:

mario
  • 144,265
  • 20
  • 237
  • 291