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