【问题标题】:preg_match_all query to only display the outer group having issuespreg_match_all 查询仅显示有问题的外部组
【发布时间】:2021-08-02 14:42:34
【问题描述】:

我不知道如何只显示 preg 查询的外部组级别,我给你举个例子。

preg_match_all('/start(.*?)end/', $input, $matches);

这个输入start1 start2 2end 1end 产生这个输出start1 start2 2end

如您所见,它没有正确分组 - 我做错了什么?

【问题讨论】:

    标签: php regex preg-match-all


    【解决方案1】:

    也许您需要在.* 中添加点贪婪,而不是懒惰。举个例子:

    $input = "blah blah start1 hello start2 blah 2end 1end";
    preg_match_all('/start(.*)end/', $input, $matches);
    print_r($matches[0][0]);
    

    打印出来:

    start1 hello start2 blah 2end 1end
    

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多