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

regular expression match match until special text is found

我有这段文字:

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 -##.

请检查:

$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"; 

试试这个:

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

您的结果应该在 $matches[1] 中找到。