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

Ant*_*rov 3 regex perl pcre

好吧,还有其他方法(嗯......或者更确切地说是工作方式),但问题是为什么这个失败?

/
\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
Run Code Online (Sandbox Code Playgroud)

无法将字符串与嵌套括号匹配:"(())"

小智 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";
}
Run Code Online (Sandbox Code Playgroud)