2011-12-01 90 views
-2

我会抓住“[email protected]”和“此电子邮件(和附件)之间的任何文本是保密的,专有的”,但不知道什么是最好和最可靠的方法来做到这一点。我认为正则表达式,但也许有人知道更好。空行数可能会增加或减少,但这两个字符串之间总是会有文本。在这种情况下,文本是“testttinggg”正则表达式为此工作?

---------- Forwarded message ---------- From: person name Date: 2011/12/1 Subject: RE: this is a test subject To: [email protected] 

    testingggggg 

    This e-mail (and attachment(s)) is confidential, proprietary, 

我试着写

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

,但它没有工作..

+2

在那里有一个具体的问题,或只是“你能为我做这个” –

+0

如果有人用文字'Hey [email protected]发送电子邮件,这封电子邮件(和附件)是保密的,专有的,但我只是想说你好!' –

+0

它不会发生,这有更多的背景发生,但我写了正则表达式,并没有工作...我试过 preg_match('/ nadal @ gmail \ .com(。*?)这封电子邮件\(和附件\(s \ )\)是保密的,/',$ wholeBody,$ matches); echo $ matches [1]; – Bulvak

回答

0

你与你的正则表达式的正确方法。您必须记住,点.默认不包含换行符,因此您必须添加DOTALL修饰符。

你可以试试这个。在capturegroup 1你有内容(不带空格空换行和)

(?<[email protected])\s*(.*?)\s*(?=This e-mail \(and attachment\(s\)\) is confidential, proprietary,) 

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

更新:

preg_match('/(?<[email protected])\s*(.*?)\s*(?=This e-mail \(and attachment\(s\)\) is confidential, proprietary,)/s', $message, $matches); 

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

+0

试过这个aint working ... preg_match('/(?<= [email protected])\ s *(。*?)\ s *(?=此电子邮件\(和附件\(s \)\)是保密的,专有的)/',$ message,$ matches); print_r($ matches); – Bulvak

+0

看到我的更新。我添加了包含换行符的's'修饰符。而且你错过了在括号之前复制反斜杠。 – Marcus

+0

工作! 1件事,但它也粘贴“”> [email protected]“ – Bulvak