正则表达式匹配匹配,直到找到特殊文本

时间:2015-02-25 14:02:42

标签: php regex preg-match preg-match-all

我有这样的文字:

Second reply. #2

Met vriendelijke groet,
{sender name}

{company details}

On 25-02-15 14:38, {email name} wrote:
> [Ticket ID: 54e70396110971424425] Re: 54e70396110,678567.971424425
>
> ##- Please type your reply above this line -##
>
> Your request (54e70396110971424425) has been deemed solved. To reopen, 
> please reply to this email or login on {name} to reply.
>
> ------------------------------------------------------------------------
>
> Feb 25, 14:38
>
> Dear Test user,
>
> First reply. #1
>
>
> {name}
> {company details}
>

我需要正则表达式才能匹配:

Second reply. #2

Met vriendelijke groet,
{sender name}

{company details}

发现时会停止匹配:

On 25-02-15 14:38, {email name} wrote:

或者当发现时:

##- Please type your reply above this line -##

我自己对正则表达式的了解不够,所以我希望有人能帮助我解决这个问题。 此文本是通过电子邮件接收的,因此对我来说最重要的是匹配此##- Please type your reply above this line -##

2 个答案:

答案 0 :(得分:2)

请检查:

$re = "/^(.*?)(?=On\\s+\\d{2}\\-\\d+\\-\\d+|\\#{2}\\-.*?\\-\\#{2})/s"; 
$str = "YOUR_INPUT_STRING"; 

preg_match($re, $str, $matches);

请参阅Demo here

如果>之前的##-不匹配,请使用^

$re = "/^(.*?)(?=On\\s+\\d{2}\\-\\d+\\-\\d+|\^\s*\\#{2}\\-.*?\\-\\#{2})/s"; 

答案 1 :(得分:1)

试试这个:

preg_match('#^(.*)\#\#- Please type your reply#s', $string, $matches);

您的结果应该在$matches[1]

中找到