contents   index   previous   next



Regular expression replacement characters

All of the special characters that have been discussed so far pertain to regular expression patterns, that is, to finding and matching strings and patterns in a target string. If all you want to do is find text, then you do not need to know about regular expression replacement characters. However, most people not only want to do powerful searches, but they also want to make powerful replacements of found text. This section describes special characters that are used in replacement strings and that are related to special characters used in search patterns.

 

Expression

 

Meaning

 

$1, $2 ... $9

 

The text that is matched by sub patterns inside of parentheses. For example, $1 substitutes the text matched in the first parenthesized group in a regular expression pattern. See the groups, (...), (?:...), (?=...), and (?!...), under regular expression reference characters.

 

$+

 

The text matched by the last group, that is, parenthesized sub pattern.

 

$`

 

The text before, to the left of, the text matched by a pattern.

 

$'

 

The text after, to the right of, the text matched by a pattern

 

$&

 

The text matched by a pattern

 

\$

 

A literal dollar sign, $.

 


Regular expression precedence