为什么 perl 使用 \g1 进行反向引用而其他人都使用 ?

问题描述 投票:0回答:1

我很好奇 Perl 正则表达式反向引用和其他人的(C++、grep、emacs,实际上是我见过的所有其他用法)之间语法差异的历史原因。

Perl 使用

\g1
进行组反向引用。其他人都使用看起来更简洁的语法,只是
\1

regex perl
1个回答
13
投票

事实上,Perl 确实接受

\1

/^(.)\1\z/s    # Equiv* to length($_) == 2 && substr($_, 0, 1) eq substr($_, 1, 1)

\g
是一个更新且更加灵活的补充。

\1             # Matches the text captured by the nth capture
\g{1}   \g1    # Matches the text captured by the nth capture
\g{-1}  \g-1   # Matches the text captured by the nth capture before the \g
\g{name}       # Matches the text captured by (?<name>...)
© www.soinside.com 2019 - 2024. All rights reserved.