Relative Backreferences
Some applications support relative backreferences. These use a negative number to reference a group preceding the backreference. To find the group that the relative backreference refers to, take the absolute number of the backreference and count that many opening parentheses of (named or unnamed) capturing groups starting at the backreference and going from right to left through the regex. So (a)(b)(c)\k<-1>
matches abcc
and (a)(b)(c)\k<-3>
matches abca
. If the backreference is inside a capturing group, then you also need to count that capturing group’s opening parenthesis. So (a)(b)(c\k<-2>)
matches abcb
. (a)(b)(c\k<-1>)
either fails to match or is an error depending on whether your application allows nested backreferences.
The syntax for nested backreferences varies widely. It is generally an extension of the syntax for named backreferences. Ruby 1.9 and later support \k<-1>
and \k'-1'
. Though this looks like the .NET syntax for named capture, .NET itself does not support relative backreferences.
Perl 5.10, PCRE 7.0, PHP 5.2.2, and R support \g{-1}
and \g-1
.
Boost supports the Perl syntax starting with Boost 1.42. Boost adds the Ruby syntax starting with Boost 1.47. To complicate matters, Boost 1.47 allowed these variants to multiply. Boost 1.47 and later allow relative backreferences to be specified with \g
or \k
and with curly braces, angle brackets, or quotes. That makes six variations plus \g-1
for a total of seven variations. This puts Boost in conflict with Ruby, PCRE, PHP and R which treat \g
with angle brackets or quotes and a negative number as a relative subroutine call.