Regular Expression Replacement Operators – %n Table
Below is a table listing the %n regular expression replacement codes for the counterpart regular expression search terms. Up to 31 search terms are addressable within a single replacement string. If all this is meaningless to you, scroll down for some examples and additional notes.
Search and Replace %n Codes | ||
1 – %1 2 – %2 3 – %3 4 – %4 5 – %5 6 – %6 7 – %7 8 – %8 9 – %9 10 – %: |
11 – %; 12 – %< 13 – %= 14 – %> 15 – %? 16 – %@ 17 – %A 18 – %B 19 – %C 20 – %D |
21 – %E 22 – %F 23 – %G 24 – %H 25 – %I 26 – %J 27 – %K 28 – %L 29 – %M 30 – %N 31 – %M |
Details:
Regular expression replacement terms use a ‘%n” syntax to reference the various regular expression search operators. The %n terms move up the ASCII, beginning with 1. They can be put in any order in your replacement string.
Dealing with a large number of %n terms can make for a very complex replacement term. Our Regular Expression Wizard may be helpful – it breaks out the number equivalents in the search term. Also consider using multiple steps to tackle your operation, perhaps via a multi-step script search-replace. In some cases several passes with simpler expressions is faster than one pass through your files with a highly complex expression.
If you use a %n that does not exist in your search term, no harm will come. Try to avoid this though. (See below)
Below are some examples showing the correspondence between %n terms and the search term operators. S: and R: here mean ‘Search for’ and ‘Replace with’. If you are experimenting, don’t actually type S: or R:
|
Possible |
|
S:?at |
Cat –> Cat |
%1 in the replacement references the ? term in the search string. |
S:?a? |
Cat –> taC |
There are two operators in the search, therefore two %n’s are possible in the replace. Here we are swapping the letters during the replace. |
S:Cat*[-]+[0-9] & +[a-zA-Z]+[0-9\-] |
Cat-6 & dog5 –> 12 <tab> 5 |
Count the number of regular search terms. There are 4: In case you are wondering, \t is the regular expression code for “tab”. |
S:???+[0-9] |
Cat5 –> 5 |
Here we used %8 in the replacement, even though there are only 4 search terms. Nothing will be output for %8. You should try to avoid these situations though. |