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

时间:2010-06-04 11:26:16

标签: 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

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

2 个答案:

答案 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中。但不是在PCRE。所以,问题是变成“Perl和PCRE正则表达式模式匹配之间有什么区别?”

瞧!有一个答案:

  

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

谢谢!