preg_match 正则表达式、一个新行但不是两个新行、空格等

发布于 2024-12-08 10:09:14 字数 1531 浏览 0 评论 0原文

我有这个文件/字符串:

   ----- 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高跟鞋的旋律 2024-12-15 10:09:14

有两件事:

将其放在一起:

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

另请注意:如果您尝试将“--p94IAEl4012027.1317751814/foo.com”作为结果的一部分,请注意您正在寻找的是三个换行符行,不是两个。换句话说:两个空行==三个换行符。

Two things:

Putting it together:

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

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.

万水千山粽是情ミ 2024-12-15 10:09:14

我能想到的另一个问题是您正在寻找 \n\n。然而,网络传输数据的换行符通常是 CRLF。因此,您应该为末尾出现 \r 做好准备:

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

您可能还想使用 .*? 而不是 .*,或者也许.*+

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:

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

You might also want to use .*? instead of .*, or maybe .*+

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文