1

I have this complex PHP I copied online:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

How do I run it in PHP? I try this:

$test = 'Joe Doe <doe@example.com>, postmaster@example.com, root, "Bob Smith" <bob@smith.com>';
$regex = '/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/';
print preg_match_all($regex, $test , $matches);
print_r($matches);

I try to escape it, but the complexity escapes me instead.

EDIT: Online Regex Tester show this regex is correct, I just don't know how to get it into PHP:

http://regexpal.com/?flags=g&regex=%5Ba-z0-9!%23%24%25%26%27*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B(%3F%3A%5C.%5Ba-z0-9!%23%24%25%26%27*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B)*%40(%3F%3A%5Ba-z0-9%5D(%3F%3A%5Ba-z0-9-%5D*%5Ba-z0-9%5D)%3F%5C.)%2B%5Ba-z0-9%5D(%3F%3A%5Ba-z0-9-%5D*%5Ba-z0-9%5D)%3F&input=Joe%20Doe%20%3Cdoe%40example.com%3E%2C%20postmaster%40example.com%2C%20root%2C%20%22Bob%20Smith%22%20%3Cbob%40smith.com%3E
Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174
  • 1
    I'd be very wary of using code that you find online without understanding what it actually does. – Andy Lester Jan 16 '14 at 19:11
  • I totally agree, but this is just a regex, and it works, and you know, real world time pressure, etc. – Bill Software Engineer Jan 16 '14 at 19:13
  • 1
    You say "it works", but if you don't know *how* it works, how do you know it works all the ways that you need it to? Don't fool yourself into thinking it's "just a regex". **Regular expressions are code.** – Andy Lester Jan 16 '14 at 19:16
  • possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – tripleee Jan 17 '14 at 07:13
  • @tripleee: No, this is not a duplicate of that question. In this question, the OP is trying to **extract** the email addresses from the given text, using a regular expression he already found online. The problem here is that the regex works on the online demo, but not when tried with PHP. – Amal Murali Jan 18 '14 at 10:19
  • I was mainly pointing out another, better regex for this purpose. This one diasallows adjacent dots in the localpart for no good reason, for example. Also, it seems to allow a completely empty domain part, if I'm reading it correctly. Anyway, don't reinvent the wheel (poorly). – tripleee Jan 18 '14 at 13:50

2 Answers2

5

You are using a forward slash / as the delimiter. Since you're using the / character inside the expression, you need to escape it using a backslash character - so, instead of /, you'll need to write \/.

The corrected version would be:

$regex = '/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/';

Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • I'd usually recommend using another delimiter to save having to escape it, but it looks like in this case, you have almost all the special characters in your string! – Pez Cuckow Jan 16 '14 at 19:10
  • 1
    @PezCuckow: Indeed. I couldn't find any such characters either :-) – Amal Murali Jan 16 '14 at 19:11
0

I use a regex processing tool here. String utilities do all this for you.

Delimeter possiblities:

 ~

 '~[a-z0-9!#$%&\'*+/=?^_`{|}\~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}\~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?~'


 /

 '/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/'


 @

 '@[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*\@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?@'


 <>

 '<[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?>'


 ;

 ';[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?;'


 #

 '#[a-z0-9!\#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!\#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?#'