gpt4 book ai didi

php - 使用正则表达式获取标签内的引​​号

转载 作者:可可西里 更新时间:2023-11-01 01:16:07 25 4
gpt4 key购买 nike

你好。我试图在特定的开始-结束字符串中获取所有引号。假设我有这个字符串:

`Hello "world". [start]this is a "mark"[end]. It should work with [start]"several" "marks"[end]`

现在我希望 [start] .. [end] 中的每个 "都被 " 替换:

$string = 'Hello "world". [start]this is a "mark"[end]. It should work with [start]"several" "marks"[end]';
$regex = '/(?<=\[start])(.*?)(?=\[end])/';
$replace = '&quot;';

$string = preg_replace($regex,$replace,$string);

这匹配 [start] 和 [end] 之间的文本。但是我想匹配其中的 ":

//expected: Hello "world". [start]this is a &quot;mark&quot;[end]. It should work with [start]&quot;several&quot; &quot;marks&quot;[end]

有什么想法吗?

最佳答案

(?s)"(?=((?!\[start\]).)*\[end\])

Live demo

解释:

 (?s)                       DOT_ALL modifier
" Literal "
(?= Begin lookahead
( # (1 start)
(?! \[start\] ) Current position should not be followed by [start]
. If yes then match
)* # (1 end)
\[end\] Until reaching [end]
) End lookahead

PHP live demo

关于php - 使用正则表达式获取标签内的引​​号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38568714/

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