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

Why does perl use \g1 for backreferences and everyone else uses \1?

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

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

实际上,Perl 确实接受 </code>。</p> <pre><code>/^(.)\z/s # Equiv* to length($_) == 2 && substr($_, 0, 1) eq substr($_, 1, 1)

\g 是更新更灵活的添加。

             # References the text captured by the nth capture
\g{1}   \g1    # References the text captured by the nth capture
\g{-1}  \g-1   # References the text captured by the nth capture before the \g
\g{name}       # References the text captured by (?<name>...)