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
我需要获取“会话记录如下---”之后的部分,直到空白新行(或者我认为是双 new_line )。
我尝试了类似的操作
<?php preg_match("/----- Transcript of session follows -----\n(.*)\n\n/",$email, $transcript_matches);?>
,但不正确,而不是 .*
我可能需要 任何字符或新行,但不是两个新行
。就在它之后两行新行
。我该怎么写呢?
谢谢。
I have this file/string:
----- 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
And I need to get the part after "transcript of session follows---", up to the blank new line (or double new_line I think).
I tried something like this
<?php preg_match("/----- Transcript of session follows -----\n(.*)\n\n/",$email, $transcript_matches);?>
but is incorrect, instead of .*
I probably need any char OR new line but NOT two new lines
. And right after it two new lines
. How can I write that?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两件事:
//s
修饰符来指定.
可以匹配换行符。请参阅 http://php.net/manual/en/reference.pcre。 pattern.modifiers.php 有关 php 中正则表达式修饰符的详细信息。.*?
指定非贪婪匹配(它将匹配它找到的最短字符串)。将其放在一起:
另请注意:如果您尝试将“--p94IAEl4012027.1317751814/foo.com”作为结果的一部分,请注意您正在寻找的是三个换行符行,不是两个。换句话说:两个空行==三个换行符。
Two things:
//s
modifier to specify that.
can match newlines. See http://php.net/manual/en/reference.pcre.pattern.modifiers.php for details on regex modifiers in php..*?
to specify a non-greedy match (it will match the shortest string it finds).Putting it together:
Also note: If you are trying to get "--p94IAEl4012027.1317751814/foo.com" as part of your results, then notice that it is three newlines lines you are looking for, not two. In other words: two blank lines == three newline characters.
我能想到的另一个问题是您正在寻找
\n\n
。然而,网络传输数据的换行符通常是 CRLF。因此,您应该为末尾出现\r
做好准备:您可能还想使用
.*?
而不是.*
,或者也许.*+
Another problem I could think of is that you are looking for
\n\n
. Linebreaks for network-transferred data is commonly CRLF however. So you should prepare for the presence of\r
at the end:You might also want to use
.*?
instead of.*
, or maybe.*+