Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to parse Email Form "To" field

If there is one that could handle this, what would be the correct regex pattern to extract email addresses from a string coming from an email form "To" line, that allows the addresses to be delimited by commas ",", semicolons ";", spaces, or any combination of the three. The regex also has to be able to ignore "noise" text, such as if an address is enclosed in "<" and ">" characters, or has an actual name next to the email address. For example, from this string that was in the To field:

"Joe Smith" <[email protected]>, [email protected]; [email protected] [email protected]

The pattern should be able to return the following matches of: jsmith@example, [email protected], [email protected], [email protected]

I am using PHP, so if this can't be done in single regex then im definitely open to other PHP-based solutions.

Thanks

like image 601
Bill Dami Avatar asked Oct 07 '10 21:10

Bill Dami


2 Answers

Try

\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b

(courtesy of RegexBuddy) as in

preg_match_all('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

Note the /i modifier to make it case-insensitive.

See also this question for an explanation of the drawbacks of regexes for finding e-mail addresses in a string.

like image 168
Tim Pietzcker Avatar answered Sep 27 '22 23:09

Tim Pietzcker


I got the regex from http://www.webcheatsheet.com/php/regular_expressions.php, and only modified it slightly.

$string = '"Joe Smith" <[email protected]>, [email protected]; [email protected] [email protected]';
$email_regex = "/[^0-9< ][A-z0-9_]+([.][A-z0-9_]+)*@[A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}/";
preg_match_all($email_regex, $string, $matches);
$emails = $matches[0];

Now $emails will have an array with all your email addresses.

like image 25
cambraca Avatar answered Sep 27 '22 21:09

cambraca