用于解析电子邮件表单“收件人”的正则表达式场地

发布于 2024-09-26 17:47:27 字数 1281 浏览 6 评论 0原文

如果有一个可以处理这个问题,从电子邮件表单“收件人”行的字符串中提取电子邮件地址的正确正则表达式模式是什么,该模式允许用逗号“,”,分号“;”分隔地址。 、空格或三者的任意组合。正则表达式还必须能够忽略“噪音”文本,例如地址是否包含在“<”中和“>”字符,或在电子邮件地址旁边有实际姓名。例如,从 To 字段中的字符串:

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

该模式应该能够返回以下匹配项: jsmith@example,[电子邮件受保护][电子邮件受保护], [email protected]

我正在使用 PHP,所以如果这不能在单个正则表达式中完成,那么我绝对愿意接受其他基于 PHP 的解决方案。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-10-03 17:47:27

尝试

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

(由 RegexBuddy 提供),如

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

注意 /i 修饰符以使其大小写-不敏感。

另请参阅此问题以了解正则表达式的缺点用于在字符串中查找电子邮件地址。

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.

寻找一个思念的角度 2024-10-03 17:47:27

我从 http://www.webcheatsheet.com/php/regular_expressions.php,仅稍作修改。

$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];

现在 $emails 将有一个包含您所有电子邮件地址的数组。

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.

稚然 2024-10-03 17:47:27

虽然您的问题特定于 RegEx 并且 Tim 给了您一个很好的答案,但对于寻找简单解决方案的人来说,请查看页面上的 mailparse_rfc822_parse_addresses http://php.net/manual/en/function.mailparse-rfc822-parse-addresses.php

请注意,这不是标准 PHP 函数,需要安装扩展。经济托管解决方案可能不允许您安装 PECL 扩展。

While your question was specific to RegEx and Tim gave you a great answer, for people looking for a simple solution, look at mailparse_rfc822_parse_addresses on page http://php.net/manual/en/function.mailparse-rfc822-parse-addresses.php

Note this is not a standard PHP function and requires installing the extension. Economy hosting solutions may not allow you to install the PECL extension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文