在下面为什么条件评估为 false
?
$_ = "aa11bb";
if(/(.)\111/){
print "It matched!\n";
}
\11
或 \111
是否具有特殊含义,即 Perl
无法“看到” \1
?
最佳答案
实际上 Perl 将 \111
解释为八进制,这在您的
字符串。如果这样的话,它只会考虑两位或更多位的反向引用
找到组数。为避免歧义,请使用 \g
或 \g{}
。引用
文档( perlre - Capture Groups ):
The \g and \k notations were introduced in Perl 5.10.0. Prior to that there were no named nor relative numbered capture groups. Absolute numbered groups were referred to using \1 , \2 , etc., and this notation is still accepted (and likely always will be). But it leads to some ambiguities if there are more than 9 capture groups, as \10 could mean either the tenth capture group, or the character whose ordinal in octal is 010 (a backspace in ASCII). Perl resolves this ambiguity by interpreting \10 as a backreference only if at least 10 left parentheses have opened before it. Likewise \11 is a backreference only if at least 11 left parentheses have opened before it. And so on. \1 through \9 are always interpreted as backreferences. There are several examples below that illustrate these perils. You can avoid the ambiguity by always using \g{} or \g if you mean capturing groups; and for octal constants always using \o{} , or for \077 and below, using 3 digits padded with leading zeros, since a leading zero implies an octal constant.
关于regex - 下面的捕获组符号对 Perl 有什么意义吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18066826/