Regular expressions 8. Substitution special characters

The substitution string is mostly interpreted as ordinary text except for the special control characters, the octal and hexadecimal escapes and the following character combinations:

\1 ... \9 are backreferences at sub-expressions 1 ... 9 in the match.
Any of the first nine sub-expressions of the match string can be inserted into the replacement string by inserting a `\' followed by a digit from 1 to 9 that represents the string matched by a parenthesized expression within the regular expression. The numbering is left to right.
    Example: A search for "(a)(b)" in the string "abc", followed by a replace "\2\1" results in "bac".
& reference at entire match.
The entire string that was matched by the search operation will be substituted.
    Example: a search for "." in the string "abcd" followed by the replace "&&" doubles every character in the result "aabbccdd".
\U \u to uppercase.
The text inserted by "&" or "\1" ... "\9" is converted to uppercase ("\u" only changes the first character to uppercase).
    Example: A search for "(aa)" in the string "aabb", followed by a replace "\U\1bc" results in the string "AAbcbb".
\L \l to lowercase.
The text inserted by "&" or "\1" ... "\9" is converted to lowercase ("\l" only changes the first character to lowercase).
    Example: A search for "(AA)" with a replace "\l\1bc" in the string "AAbb" results in the string "aAbcbb".

Links to this page


© djmw, July 8, 2001