Quick Start | Tutorial | Tools & Languages | Examples | Reference | Book Reviews |

RegexBuddy—Better than a regular expression tutorial!

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. JGsoft V2 and 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, R, and JGsoft which treat \g with angle brackets or quotes and a negative number as a relative subroutine call.

Make a Donation

Did this website just save you a trip to the bookstore? Please make a donation to support this site, and you'll get a lifetime of advertisement-free access to this site!

Quick Start | Tutorial | Tools & Languages | Examples | Reference | Book Reviews |

Introduction | Table of Contents | Special Characters | Non-Printable Characters | Regex Engine Internals | Character Classes | Character Class Subtraction | Character Class Intersection | Shorthand Character Classes | Dot | Anchors | Word Boundaries | Alternation | Optional Items | Repetition | Grouping & Capturing | Backreferences | Backreferences, part 2 | Named Groups | Relative Backreferences | Branch Reset Groups | Free-Spacing & Comments | Unicode | Mode Modifiers | Atomic Grouping | Possessive Quantifiers | Lookahead & Lookbehind | Lookaround, part 2 | Keep Text out of The Match | Conditionals | Balancing Groups | Recursion | Subroutines | Infinite Recursion | Recursion & Quantifiers | Recursion & Capturing | Recursion & Backreferences | Recursion & Backtracking | POSIX Bracket Expressions | Zero-Length Matches | Continuing Matches |