用于解析电子邮件表单“收件人”的正则表达式场地
如果有一个可以处理这个问题,从电子邮件表单“收件人”行的字符串中提取电子邮件地址的正确正则表达式模式是什么,该模式允许用逗号“,”,分号“;”分隔地址。 、空格或三者的任意组合。正则表达式还必须能够忽略“噪音”文本,例如地址是否包含在“<”中和“>”字符,或在电子邮件地址旁边有实际姓名。例如,从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
(由 RegexBuddy 提供),如
注意
/i
修饰符以使其大小写-不敏感。另请参阅此问题以了解正则表达式的缺点用于在字符串中查找电子邮件地址。
Try
(courtesy of RegexBuddy) as in
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.
我从 http://www.webcheatsheet.com/php/regular_expressions.php,仅稍作修改。
现在 $emails 将有一个包含您所有电子邮件地址的数组。
I got the regex from http://www.webcheatsheet.com/php/regular_expressions.php, and only modified it slightly.
Now $emails will have an array with all your email addresses.
虽然您的问题特定于 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.