我得之間的任何文本:正則表達式解析最終收件人的電子郵件標題
最終收件人:RFC822; !這裏!動作
我需要!在這裏!從這個例子。可能有任何字符串。
我想是這樣的:
$Pattern = '/Final-Recipient: RFC822; (.*) Action/';
但它不工作。
UPD
這裏是我試圖解析字符串:既然你說 「任何字符串」,它可以包含空格,最接近的近似會http://dpaste.com/187638/
我得之間的任何文本:正則表達式解析最終收件人的電子郵件標題
最終收件人:RFC822; !這裏!動作
我需要!在這裏!從這個例子。可能有任何字符串。
我想是這樣的:
$Pattern = '/Final-Recipient: RFC822; (.*) Action/';
但它不工作。
UPD
這裏是我試圖解析字符串:既然你說 「任何字符串」,它可以包含空格,最接近的近似會http://dpaste.com/187638/
你的模式能正常工作對我來說:
$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」出現在消息的其他地方,則會導致正則表達式失敗。匹配點是危險的。如果可能,嘗試找到更具體的模式。
說實話,可能只有電子郵件地址。任何換行符。 – Ockonal 2010-04-25 19:55:02
$Pattern = '/Final-Recipient: RFC822; (.*?) Action/s';
# ^ ^
# lazy match instead of greedy match ----' |
# allow . to match newline -----'
當然它不會匹配"Final-Recipient: RFC822; Action Action"
。
請更新。這個正則表達式不適用於給定的文本。 – Ockonal 2010-04-25 19:52:39
我打算建議一種不使用它們的方法,這需要額外的繁瑣工作。
<?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 [email protected] ----- 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: <[email protected]*.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);
有點晚了....
但已被要求在如何解決的一個問題,是不是挺他的要求作品或許已經加入了多行到一行條款?(恕我直言)
這可能幫助別人....
我假設OP試圖解析傳遞狀態通知的最終收件人頭字段。
爲最終收件人場的規範可以在這裏看到:http://tools.ietf.org/html/rfc3464#page-15
如果問題被分解,運能拉動最終收件人字段作爲一個字段(最終收貨人後跟一個字符/空白行在下一行。
如
Original-recipient: rfc822;[email protected]
Final-recipient: rfc822;[email protected]
Action: failed
Status: 5.1.1 (Remote SMTP server has rejected address)
最終收貨人之後的下一個領域,行動具有下一行的開始。即不是後面有一個空格或空行。
然後他所要做的就是分開線路;並採取第二部分
即
String[] twoparts = "Final-recipient: rfc822;[email protected]".split(";",2) // 2 here means (2-1) = 1 match
String email = twoparts[1]
你是如何定義的結束!在這裏!?它總是在驚歎號嗎? – 2010-04-25 19:44:12
之後需要文字'Action /' – Ockonal 2010-04-25 19:45:09
斜槓不在您的原始文章 – 2010-04-25 19:47:31