Prev: Next: , Up: String Expansions[Contents][Index]


9.3.1 Backreference expansion

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.

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.