Regular Expression Replacement Operators - %n Table

image\regex2_shg.gif

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:


Strings

Possible
Replace Effect


Explanation

S:?at
R:%1at

Cat --> Cat

%1 in the replacement references the ? term in the search string.

S:?a?
R:%2a%1

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\-]
R:%3 \t %4

Cat-6 & dog5 --> 12 <tab> 5
Cat-0 & bird1 --> 0 <tab> 1

Count the number of regular search terms. There are 4:
*[-], +[0-9], +[a-zA-Z], and +[0-9\-]. This means %1, %2, %3, %4 can be used in the replacement string. Here we are getting rid of the original text strings and the - character (if present) and just returning the numbers, separated by a <tab> character.

In case you are wondering, \t is the regular expression code for "tab".

S:???+[0-9]
R:%8%4

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.