gpt4 book ai didi

PHP - Preg_match 最短匹配

转载 作者:可可西里 更新时间:2023-10-31 23:18:30 27 4
gpt4 key购买 nike

我正在尝试从此字符串中获取“内容”:

$string = "start start content end";

像这样使用 preg_match:

preg_match('/start(.*?)end/', $string, $matches);
echo $match[1];

但是,问题是 $matches[1] 返回 start content 而不仅仅是 content 因为有两个 start$string 中(也许更多)

如何使用preg_match只获取content部分?

最佳答案

使用否定前瞻:

$string = "start start content end";
preg_match('/start\h*((?:(?!start).)*)end/', $string, $matches);
echo $matches[1];
// content

(?:(?!start).) 将匹配任何后面没有 start 的字符。

关于PHP - Preg_match 最短匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340462/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com