I am curious about the historical reason for the divergence in syntax between Perl regex backreferences and everyone else's (C++, grep, emacs, literally every other usage I've seen).
Perl uses \g1
for a group backreference. Everyone else uses a syntax which seems much cleaner, just \1
.
Actually, Perl does accept \1
.
/^(.)\1\z/s # Equiv* to length($_) == 2 && substr($_, 0, 1) eq substr($_, 1, 1)
\g
is a far more recent and far more flexible addition.
\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>...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.