13.2 Replacement text

When performing a search-and-replace operation, the text that is used to replace a matched pattern is usually just literal text. However, it is also possible to use a special escape sequence within the replacement text that represents part of the matched pattern.

When parentheses are used in a pattern to delimit sub-patterns, each sub-pattern may be referred to in the replacement text. The special escape sequence \1 refers to the first sub-pattern (reading from the left); \2 refers to the second sub-pattern, and so on. These are referred to as backreferences.

Within an R expression, the backslash character must be escaped as usual, so the replacement text referring to the first sub-pattern would have to written like this: "\\1".

An example of the use of backreferences is given in Section 11.9.1.



Paul Murrell

Creative Commons License
This document is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.