gpt4 book ai didi

php - 在正则表达式中重用匹配组

转载 作者:行者123 更新时间:2023-12-03 19:37:52 25 4
gpt4 key购买 nike

我正在尝试匹配:“{{ loop (something) }}”和第一个 {{/loop (that something) }}

之间的所有内容

文本示例:

{{ LOOP data }}
<span> Member key = {{.key}} </span>
<span> Member value = {{.}} </span>
<span> Member member = {{name}} </span>
<span> Iteration number = {{.counter}} </span>
{{ /LOOP data }}

这是正则表达式不起作用:

/\{\{\s+loop\s+(.+?)\s+\}\}([\w\W]+?)\{\{\s+\/loop\s$1\s+\}\}/i

如您所见,我正在尝试重用第一个匹配组和“$1”:我错过了什么?

PHP代码如下:

$text = '
{{ LOOP data }}
<span> Member key = {{.key}} </span>
<span> Member value = {{.}} </span>
<span> Member member = {{name}} </span>
<span> Iteration number = {{.counter}} </span>
{{ /LOOP data }}
';

preg_match('/\{\{\s+loop\s+(.+?)\s+\}\}([\w\W]+?)\{\{\s+\/loop\s\1\s+\}\}/i', $text, $match);
print_r($match);
die;

最佳答案

我相信你想要 \1,而不是 $1

更多信息:http://www.regular-expressions.info/brackets.html#usebackrefinregex

关于php - 在正则表达式中重用匹配组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14318955/

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