The following examples illustrate the use of back references in a replacement string. Use the parentheses characters within the searched string to cause the subexpressions contained within the parentheses to be remembered. Subexpressions are numbered from left to right, starting with 1. Therefore, to refer to the leftmost remembered subexpression, use \1 in the replacement string, \2 for the second leftmost, and so forth.
Pattern | Searched String | Replacement | Result |
---|---|---|---|
(.+)(\d)(\.dat|\.DAT) | custfil5.dat | \1new\2\3 | Custfilenew5.dat |
(First|1st)(.+)(Last|Final) | FirstThenLast | \3\2\1 | LastThenFirst |
(First|1st)(.+)(Last|Final) | 1stThenFinal | \3\2\1 | FinalThen1st |