简体   繁体   English

Preg_match多个实例重用分隔符

[英]Preg_match multiple instances reuse delimiter

I've revised the question as I did not explain correctly the first time. 我已经修改了这个问题,因为我第一次没有正确解释。

Can someone please help me with this regex. 有人可以帮助我这个正则表达式。 I can't seem to figure out how to use the same delimeter as the end of one match and then reuse as the start of the next. 我似乎无法弄清楚如何使用相同的分隔符作为一个匹配的结束,然后重用作为下一个匹配的开始。

In the following code I'm trying to match everything in between each delimiter_test statement. 在下面的代码中,我试图匹配每个delimiter_test语句之间的所有内容。

$string = "

delimiter_test this is a test
this is more data,etc
delimiter_test this is another test
and this is more data
delimiter_test this yet another test
and this is even more data

";

Here is the regex I've tried: 这是我试过的正则表达式:

preg_match_all('/delimiter_test(.*?)delimiter_test/s', $string, $matches);

And here are my results: 这是我的结果:

Array
(
    [0] => Array
        (
            [0] => delimiter_test this is a test
this is more data,etc
delimiter_test
        )

    [1] => Array
        (
            [0] =>  this is a test
this is more data,etc

        )

)

So it only gets what is between the first and second 'delimiter_test'. 所以它只得到第一个和第二个'delimiter_test'之间的内容。

Hopefully that makes sense. 希望这是有道理的。

Thanks, Max 谢谢,马克斯

Thanks, Max 谢谢,马克斯

Updated answer: 更新的答案:

You can use Lookarounds to achieve this. 您可以使用Lookarounds来实现此目的。

preg_match_all('/(?<=delimiter_test).*?(?=delimiter_test|$)/s', $string, $matches);
print_r($matches[0]);

Working Demo 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM