gpt4 book ai didi

regex - PCRE(递归)模式匹配包含正确括号子字符串的字符串。为什么这个会失败?

转载 作者:行者123 更新时间:2023-12-01 08:19:23 28 4
gpt4 key购买 nike

好吧,还有其他方法(嗯……或者更确切地说是可行的方法)来做到这一点,但问题是为什么这个方法会失败?

/
\A # start of the string
( # group 1
(?: # group 2
[^()]* # something other than parentheses (greedy)
| # or
\( (?1) \) # parenthesized group 1
) # -group 2
+ # at least once (greedy)
) # -group 1
\Z # end of the string
/x

无法匹配带有嵌套括号的字符串:“(())”

最佳答案

它不会失败

$ perl junk.pl
matched junk >(())<

$ cat junk.pl
my $junk = qr/
\A # start of the string
( # group 1
(?: # group 2
[^()]* # something other than parentheses (greedy)
| # or
\( (?1) \) # parenthesized group 1
) # -group 2
+ # at least once (greedy)
) # -group 1
\Z # end of the string
/x;

if( "(())" =~ $junk ){
print "matched junk >$1<\n";
}

关于regex - PCRE(递归)模式匹配包含正确括号子字符串的字符串。为什么这个会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2973630/

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