-2

可能重复:
如何展开 RFC 822
Parsing e-mail-like headers(类似于 RFC822)

我有一些类似于电子邮件数据的输入数据,因为长行被换行到下一行。例如:

robot-useragent: ABCdatos BotLink/1.0.2 (test links)
robot-language: basic
robot-description: This robot is used to verify availability of the ABCdatos
                   directory entries (http://www.abcdatos.com), checking
                   HTTP HEAD. Robot runs twice a week. Under HTTP 5xx
                   error responses or unable to connect, it repeats
                   verification some hours later, verifiying if that was a
                   temporary situation.

robot-description字段对于一行来说“太长”,并且被换行到下一行。为了帮助解析这些数据,我想提出一个正则表达式,可用于preg_replace()替换以下条件:

  • 换行符后跟空格
  • 替换换行符后跟其他换行符

示例输出:

robot-description: This robot is used to verify availability of the ABCdatos directory entries (http://www.abcdatos.com), checking HTTP HEAD. Robot runs twice a week. Under HTTP 5xx error responses or unable to connect, it repeats verification some hours later, verifiying if that was a temporary situation.

我是 RegEx 的新手。我怎样才能建立这样的表达?如果您选择回答,请在表达式中包含对组件的简要说明。我真的很想学习如何做这些。

我从这个开始: \n([^\S])* 它很接近。 http://codepad.org/iMObpgFX

4

1 回答 1

1

也许你可以尝试:

(\r|\n)\s+

(\r|\n) # matches both newline and carriage return 
\s+     # any whitespace (tabs, spaces, new lines)

试试看

于 2012-10-09T17:57:24.843 回答