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

问题描述

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

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

解决方法

实际上,Perl 确实接受 \1

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

\g 是一个更新且灵活得多的附加功能。

\1             # 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>...)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...