正则表达式 - 在完整日期之后和标准文本之前获取字符串

时间:2017-02-22 04:19:04

标签: regex preg-match

我坚持使用另一个正则表达式。我正在提取电子邮件数据。在下面的示例中,只有引号中的时间,日期和消息发生变化。

Message Received 6:06pm 21st February "Hello. My name is John Smith" Some standard text.
Message Received 8:08pm 22nd February "Hello. My name is "John Smith"" Some standard text.

如果我需要从积极的lookbehind开始,(?<=Message Received )开始搜索数据的这个特定点,我怎么才能收到消息?该消息将始终以引号开头和结尾,但用户可以插入自己的引号,如第二个示例所示。

3 个答案:

答案 0 :(得分:0)

用于在双引号之间提取消息。

(?=Message Received)[^\"]+\K\"[\w\s\"\.]+\"

Regex demo

答案 1 :(得分:0)

您捕获组中的消息

(?<=Message Received)[^"]*(.*)(?=\s+Some standard text)

答案 2 :(得分:0)

您可以在捕获组中使用否定的charcter类:

/Message Received.*?"([^\n]+)"/

段:

$input = 'Message Received 6:06pm 21st February "Hello. My name is John Smith" Some standard text.
Message Received 8:08pm 22nd February "Hello. My name is "John Smith"" Some standard text.}';

preg_match_all('/Message Received.*?"([^\n]+)"/', $input, $matches);

foreach ($matches[1] as $match) {
    echo $match . "\r\n";
}

输出:

> Hello. My name is John Smith
> Hello. My name is "John Smith"