dongwen3410 2013-07-18 16:46
浏览 95
已采纳

电子邮件地址列表的正则表达式可能跨越多行

I want to scan the body of an email for email address lists from forwarded emails, like:

From: John Smith <john@example.com>
To: Jane Smith <jane@example.com>, Mary Smith
<mary@example.com>
Cc: Ed Smith <ed@example.com>
Subject: this is a test

I'm going to use Mail_RFC822::parseAddressList() to fully parse each list (there are a lot of details to get right in there, so I shouldn't try to re-engineer it), but I do want to pluck out the lines to hand off to this function. I have a simple regex that just looks for lines with email addresses, and that works most of the time.

But in the wild, there are sometimes emails like the example above, where the name and address get split onto different lines. If I do it line by line, the top half of the To: line above will fail to parse at all in parseAddressList() because a name without an address is invalid; and the bottom half will parse, but will be missing the name, which was on the previous line.

So I need a regex that can look at multiple lines at once, which complicates things beyond my expertise. An adequate solution would continue to group lines together as long as it keeps finding a basic email pattern ([\w\.\+\-]+@[\w\.\-]+\.[\w\.\-]+ ... doesn't need to be perfect) but without a word-colon combo at the beginning of the line (^\S*:) so that, as in the example above, the Cc: line is a separate match. Thanks in advance for your help.

  • 写回答

2条回答 默认 最新

  • download92000 2013-07-18 16:59
    关注

    You can pre-process the string to remove new lines before < characters and then pass the result to your parseAddressList function.

    Something like replacing /(?:? |)\s*</ with <:

    $emails = Mail_RFC822::parseAddressList(preg_replace('/(?:?
    |)\s*</', '<', $emailHeaders));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测