gpt4 book ai didi

php - 在正则表达式中循环

转载 作者:行者123 更新时间:2023-12-02 22:31:26 25 4
gpt4 key购买 nike

正则表达式能找到规律吗?

{{foo.bar1.bar2.bar3}}

在小组中的位置

$1 = foo $2 = bar1 $3 = bar2 $4 = bar3 and so on..

这就像一遍又一遍地重新执行表达式,直到无法匹配为止。

我正在处理的当前表达式是

(?:\{{2})([\w]+).([\w]+)(?:\}{2})

这是来自正则表达式的链接。

http://regexr.com?3203h

--

好吧,我想我没有很好地解释我想要在这里实现的目标。

假设我正在尝试替换所有

.barX{{foo . . . }}

我的预期结果应该是

$foo->bar1->bar2->bar3

最佳答案

假设匹配中不允许使用大括号,这应该可以工作:

preg_match_all(
'%(?<= # Assert that the previous character(s) are either
\{\{ # {{
| # or
\. # .
) # End of lookbehind
[^{}.]* # Match any number of characters besides braces/dots.
(?= # Assert that the following regex can be matched here:
(?: # Try to match
\. # a dot, followed by
[^{}]* # any number of characters except braces
)? # optionally
\}\} # Match }}
) # End of lookahead%x',
$subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

关于php - 在正则表达式中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12191051/

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