Preg_match多个实例重用分隔符

时间:2014-07-09 23:09:25

标签: php regex preg-match-all

我修改了这个问题,因为我第一次没有正确解释。

有人可以帮我解决这个正则表达式。我似乎无法弄清楚如何使用相同的分隔符作为一个匹配的结束,然后重用作为下一个匹配的开始。

在下面的代码中,我试图匹配每个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

";

这是我试过的正则表达式:

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

以下是我的结果:

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

        )

)

所以它只能获得第一个和第二个'delimiter_test'之间的内容。

希望这是有道理的。

谢谢,Max

谢谢, 最大

1 个答案:

答案 0 :(得分:2)

更新回答:

您可以使用Lookarounds来实现此目标。

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

Working Demo

相关问题