Regex是怎么工作的?


what Regex works for this?

我会抓取介于"Nadal@gmail.com"answers"此电子邮件(和附件)是机密的,专有的"之间的任何文本,但不知道什么是最好和最可靠的方法来做到这一点。我认为正则表达式,但也许有人知道更好的。空行的数量可能增加或减少,但这两个字符串之间总是有文本。在本例中,文本为"testttinggg"

---------- Forwarded message ---------- From: person name Date: 2011/12/1 Subject: RE: this is a test subject To: Nadal@gmail.com
    testingggggg
    This e-mail (and attachment(s)) is confidential, proprietary,

我试着写

       preg_match('/nadal@gmail'.com(.*?)This e-mail '(and attachment'(s')') is confidential,/', $wholeBody, $matches);     echo $matches[1];

您使用正则表达式的方式是正确的。您必须记住点.默认情况下不包括换行符,因此您必须添加DOTALL修饰符。

你可以试试这个。在捕获组1中,您将拥有内容(不含空白空格和换行符)

(?<=Nadal@gmail.com)'s*(.*?)'s*(?=This e-mail '(and attachment'(s')') is confidential, proprietary,)

你可以在这里看到它的作用:http://regexr.com?2vc19

更新:

preg_match('/(?<=Nadal@gmail.com)'s*(.*?)'s*(?=This e-mail '(and attachment'(s')') is confidential, proprietary,)/s', $message, $matches);

http://codepad.viper - 7. - com/c6mxqd