regex - 下面的捕获组符号对 Perl 有什么意义吗

标签 regex perl capture-group

在下面为什么条件评估为 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/

相关文章:

perl - 从 1000 增加限制?

python - 如何使用正则表达式从路径中提取文件名

sql - 正则表达式查询

xml - 如何使用 XML::LibXML 列出 XML 节点属性?

正则表达式:捕获组内的捕获组

c# - C# 中的正则表达式如何仅替换捕获组而不替换非捕获组

java - 将 Java 正则表达式捕获组替换为其自身的一部分

用于匹配以 # 开头的完整单词的正则表达式 (Javascript)

javascript - 如何使用 JS/jQUery 正则表达式从字符串中删除不匹配的部分

perl - 如何使用 bash 或 Perl 重新格式化 mbox 文件中的消息?