Regex for parsing the To email form

If there is one that could handle this, then what is the correct regular expression pattern to extract email addresses from the line coming from the To email line, which allows you to separate addresses with commas ",", with a comma ";", spaces or any combination of the three. The regular expression should also be able to ignore the text "noise", for example, if the address is enclosed in "<" and ">", or has the actual name next to the email address. For example, from this line, which was in the To field:

"Joe Smith" <jsmith@example.com>, kjones@aol.com; someoneelse@nowhere.com mjane@gmail.com

The sample should be able to return the following matches: jsmith @example, kjones@aol.com , someoneelse@nowhere.com , mjane@gmail.com

I use PHP, so if this cannot be done in one regex then I will definitely open up for other PHP based solutions.

thank

+5
source share
3 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];

Pay attention to the modifier /ito make it case insensitive.

See also this question for an explanation of the disadvantages of regular expressions for finding email addresses in a string.

+6

http://www.webcheatsheet.com/php/regular_expressions.php .

$string = '"Joe Smith" <jsmith@example.com>, kjones@aol.com; someoneelse@nowhere.com mjane@gmail.com';
$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];

$emails .

+1

RegEx, , , , mailparse_rfc822_parse_addresses http://php.net/manual/en/function.mailparse-rfc822-parse-addresses.php

, PHP . - PECL.

+1

All Articles