用于解析Final-Recipient电子邮件标头的正则表达式

时间:2010-04-25 19:42:37

标签: php regex

我必须在:

之间得到任何文字
  

最终收件人:RFC822; !这里!操作

我需要!HERE!来自此示例。可能有任何字符串。

我尝试过类似的事情:

$Pattern = '/Final-Recipient: RFC822; (.*) Action/';

但它不起作用。

UPD

以下是我要解析的字符串:http://dpaste.com/187638/

5 个答案:

答案 0 :(得分:2)

由于您说“任何字符串”可能包含空格,因此最接近的近似值为

$Pattern = '/Final-Recipient: RFC822; (.*?) Action/s';
#                                        ^         ^
# lazy match instead of greedy match ----'         |
#                    allow . to match newline -----'

当然它与"Final-Recipient: RFC822; Action Action"不匹配。

答案 1 :(得分:2)

你的模式适合我:

$i = 'This is a MIME-encapsulated message --o3ONXoEH01blah3:35:33 +0400 (MSD) Final-Recipient: RFC822; !HERE! Action: failed Status: 4.4.7 Lblahru> From: *
@*.ru';    
$pattern = '/Final-Recipient: RFC822; (.*) Action/';
$matches = Array();
preg_match($pattern, $i, $matches);
print_r($matches);

输出:

Array
(
    [0] => Final-Recipient: RFC822; !HERE! Action
    [1] => !HERE!
)

另请注意,如果“任何文本”包含新行,您的模式将失败。使用DOTALL modifier /.../s允许点也匹配新行。另请注意,如果文本“Action”出现在消息的其他位置,则会导致正则表达式失败。匹配点很危险。如果可能,尝试找到更具体的模式。

答案 2 :(得分:0)

我将建议一种不使用它们的方法,这需要额外的繁忙工作。

<?php
$message = 'This is a MIME-encapsulated message --o3ONXoEH016763.1272152184/zvm19.host.ru The original message was received at Fri, 23 Apr 2010 03:35:33 +0400 (MSD) from roller@localhost ----- The following addresses had permanent fatal errors ----- "Flucker" ----- Transcript of session follows ----- 451 grl.unibel.by: Name server timeout Message could not be delivered for 2 days Message will be deleted from queue --o3ONXoEH016763.1272152184/*.host.ru Content-Type: message/delivery-status Reporting-MTA: dns; zvm19.host.ru Arrival-Date: Fri, 23 Apr 2010 03:35:33 +0400 (MSD) Final-Recipient: RFC822; !HERE! Action: failed Status: 4.4.7 Last-Attempt-Date: Sun, 25 Apr 2010 03:36:24 +0400 (MSD) --o3ONXoEH016763.1272152184/zvm19.host.ru Content-Type: message/rfc822 Content-Transfer-Encoding: 8bit Return-Path: Received: (from *@localhost) by *.host.ru (8.13.8/Zenon/Postman) id o3MNZX5h059932; Fri, 23 Apr 2010 03:35:33 +0400 (MSD) (envelope-from *@roller.ru) Date: Fri, 23 Apr 2010 03:35:33 +0400 (MSD) Message-Id: <201004222335.o3MNZX5h059932@*.host.ru> From: *
@*.ru';
$left_delimiter = 'Final-Recipient: RFC822; ';
$right_delimiter = ' Action';
$left_delimiter_pos = strrpos($message, $left_delimiter);
$right_delimiter_pos = strpos($message, $right_delimiter);
$desired_message_fragment = '';
if ($left_delimiter_pos !== false && $right_delimiter_pos !== false) {
    $fragment_start = $left_delimiter_pos + strlen($left_delimiter);
    $fragment_length = $right_delimiter_pos - $fragment_start;
    $desired_message_fragment = substr(
        $message, $fragment_start, $fragment_length
    );
}
var_dump($desired_message_fragment);

答案 3 :(得分:0)

有点晚......

但是有人问过如何解决一个不太符合他要求的问题Op或许已将多行连接到一行上了?(imho)。

这可能有助于其他人......

我假设op正在尝试解析传递状态通知的Final-Recipient头字段。

最终收件人字段的规范可以在这里看到:http://tools.ietf.org/html/rfc3464#page-15

如果问题被解决,op可以将最终收件人字段作为单个字段拉出(最后收件人后跟下一行的字符/空行。

e.g。

Original-recipient: rfc822;some-email-that-does-not-exist@gmail.com
Final-recipient: rfc822;some-email-that-does-not-exist@gmail.com
Action: failed
Status: 5.1.1 (Remote SMTP server has rejected address)

最终收件人之后是下一个字段的开始,即下一行中有A的Action。即没有后跟空格或空行。

然后,他所要做的就是将线分开;并采取第二部分

String[] twoparts = "Final-recipient: rfc822;some-email-that-does-not-exist@gmail.com".split(";",2) // 2 here means (2-1) = 1 match
String email = twoparts[1]

答案 4 :(得分:0)

$Pattern = '/Final-Recipient:[^;]+[;|<|\s]+([^\s|^<|^>]+)/i';

事实证明,以下表达式最适合我的问题,因为有时存在以下类型的行:

Final-Recipient: LOCAL;<example@rambler.ru>