Backreference is a construct that refers to a parenthesized group within a regular expression matched by one of service matching directives (see Service Selection Statements). During backreference expansion, each occurrence of such construct is replaced with the actual value of that parenthesized group.
Syntactically, backreferences can take two forms. The construct
$n
, where n is a decimal number, refers to
nth parenthesized subexpression of the most recently matched
statement, and the construct $n(m)
refers to
nth parenthesized subexpression in the mth recently matched
statement. Numbering of subexpressions starts at 1 ($0
refers
to entire matching string). Numbering of matches starts at 0.
For example, given the following statements
Host -re "www\\.(.+)" Header -re -icase "^Content-Type: *(.*)" Path "^/static(/.*)?"
$1
refers to the subgroup of Path
, $1(1)
refers to that of
Header
, and $1(2)
to that of Host
.
Conditional statements can also be tagged (see tag). When referring to a
subexpression in such a statement, the form $n(t)
can be used, where t is the tag. For example, after tagging the
statements in the example above:
Host -tag "host" -re "www\\.(.+)" Header -tag "type" -re -icase "^Content-Type: *(.*)" Path -tag "dir" "^/static(/.*)?"
the corresponding strings can be referred to as: $1
or
$1(dir)
, $1(type)
, and $1(host)
,
correspondingly.
Curly braces may be used to avoid incorrectly parsing text fragment that follows the reference as being its part. This is useful if the reference is immediately followed by a decimal digit or opening parenthesis, as in: ‘${1}(text)’.
To insert a literal dollar or percent sign in the string, use ‘$$’ or ‘$%’, correspondingly.