嗯,还有其他方法(嗯......或者更确切地说是工作方式),但问题是为什么这个失败?
/
\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
无法将字符串与嵌套括号匹配:“(())”
答案 0 :(得分:7)
它不会失败
$ 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";
}
答案 1 :(得分:4)
瞧!有一个答案:
Perl的递归差异
In PCRE (like Python, but unlike Perl), a recursive subpattern call is always treated as an atomic group. That is, once it has matched some of the subject string, it is never re-entered, even if it contains untried alternatives and there is a subsequent matching failure.
因此,我们只需要交换两个子模式:
/ \A ( (?: \( (?1) \) | [^()]* )+ ) \Z /x
谢谢!