2011-10-05 40 views
0

我有這個文件/字符串:的preg_match正則表達式,一個新的生產線,但沒有兩條新線,空格等

----- Transcript of session follows ----- 
... while talking to mta6.am0.yahoodns.net.: 
>>> DATA 
<<< 554 delivery error: dd Sorry your message to [email protected] cannot be delivered. This account has been disabled or discontinued [#102]. - mta1070.mail.ac4.yahoo.com 
554 5.0.0 Service unavailable 

--p94IAEl4012027.1317751814/foo.com 


    ----- Transcript of session follows ----- 
... while talking to mail.messaging.microsoft.com.: 
>>> DATA 
<<< 550 5.7.1 Service unavailable; Client host [foo] blocked using Blocklist 2, mail from IP banned; To request removal from this list please forward this message to [email protected] 
550 5.1.1 <[email protected]>... User unknown 
<<< 503 5.5.2 Need rcpt command 

--p94I91SC011973.1317751741/foo.com 
Content-Type: message/delivery-status 

我需要後獲得部分「的答問如下---」 ,直到空白的新行(或者我認爲是double new_line)。

我想是這樣的

<?php preg_match("/----- Transcript of session follows -----\n(.*)\n\n/",$email, $transcript_matches);?> 

,但不正確,而不是.*我可能需要any char OR new line but NOT two new lines。之後它two new lines。我怎麼寫這個?

謝謝。

+0

你需要使用一個16進制軟件,看看有什麼輸入你實際有。 – mario

回答

2

兩件事情:

將其組合在一起:

<?php preg_match("/----- Transcript of session follows -----\n(.*?)\n\n/s",$email, $transcript_matches);?> 

還要注意:如果你想獲得「--p94IAEl4012027.1317751814/foo.com」作爲結果的一部分,那麼請注意,這是你正在尋找三條新線,而不是兩條。換句話說:兩個空行==三個換行符。

+1

'/ m'多行標誌用於'^'和'$'。點匹配行爲通過'/ s' dotall標誌改變。 – mario

+0

@mario你知道規則應該是什麼樣子嗎? – adrianTNT

+0

@mario,固定。接得好。我通常選擇的編程方式是ruby,其中'm'與php的's'意思相同。 –

0

我能想到的另一個問題是您正在尋找\n\n。然而,網絡傳輸數據的線路斷點通常是CRLF。所以,你應該爲\r末存在準備:

follows -----\s*\r?\n(.*)\r?\n\r?\n/s 

您可能還需要使用.*?而不是.*,也許.*+

+0

我嘗試了許多這些組合,我無法讓它工作。最後一個是我的文件中的\ n \ n。我如何正確地寫這個? '/ -----會議記錄如下----- \ n([^ \ n \ n] *)\ n \ n /'(試圖匹配任何不是「nn」,然後是「nn」); – adrianTNT

+0

你不明白。不會有'\ n \ n' – mario