Regular Expression Replacement Operators

Search & Replace regex2 shg Replacement Operators

Replacement Operators Index

Basic Operators
   %n Operator
   < – Lower Case Operator
   > – Upper Case Operator
   %n>> / %n<< – Counter
   %n>#> / %n<#< -Counter

Special Operators
   Special Replacement Operators
   Counter Operators
   Number Parsing Replacements

Additional Issues
   Notes on Replacement Expressions
   Literal Characters
   Operations in Word Documents & binary files

Related Topics

   Regular Expression Search Operators
   Regular Expression Examples
   Regular Expressions Overview

 

Notes on Replacement Expressions:

  • The operators’ matches can be replaced with nothing (removed). The case of the text matched can be changed. Matches can also be left intact. All operators may match some strings and therefore all should be accounted for in the replace expression.

  • The following characters have special meaning in regular expression replace terms. If you wish to use the literal of one of these characters, preface them with the \ character.
        % \ < >
    For example, the % character normally denotes the %n basic replacement operator. If you want to use a literal % character in your replace string, denote that as \%. See Regular Expression Literals for more information.

  • Up to 31 parameters may be used at once by referring to those over number nine by moving up the ASCII table, e.g., 123456789:;<=>?@ABCDEFGHIJKLMN. A complete list is present in the %n table. 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.

  • To view the result of all replace operations, turn on the “Prompt on All” radio button under Replace Options.

  • You may wish to increase the “Maximum Regular Expression Size” under Search Options if you have a regular expression search that involves Binary Characters and/or is very complex.

  • Please See Word Documents Notes for important information on search & replaces in non-text files such as Word documents, Word Perfect documents, spreadsheets, etc.

 


Regular Expression Replacement Operators

%n

Basic Replacement Operator: The replacement operators use a %n convention, where n corresponds to a component in the regular expression search string. For example, %1 refers to the first expression in the search string, %2 refers to the second, and so on. The %n parameters may be used several times, omitted, or used in any order. Up to 31 parameters may be used at once by referring to those over number %9 moving up the ASCII table, e.g., 123456789:;<=>?@ABCDEFGHIJKLM.

See %n table for more information and examples.

Some examples of %n usage are:

 

Given the series:

Search Expression:
Replacement Expression:
Results:

Values:

<a href=”http://wait.com”>S1</a>
<a href=”http://waitalot.com”>S2</a>
<a href=”http://wait*[]“>*[]<
<a href=”http://wait%1“>(Slow) %2 <
<a href=”http://wait.com”>(Slow) S1 </a>
<a href=”http://waitalot.com”>(Slow) S2 </a>
%1 = *[]
%2 = *[]

 

Given the series:

Search Expression:
Replacement Expression:
Results:

Values:

<BR>
    <BR>
(TAB)<BR>
^<BR>
<BR>
<BR>
<BR>
<BR>
Here, the absence of %1 in the replace expression
  is used to eliminate spaces & tabs before <BR>.

 

Given the series:

Search Expression:
Replacement Expression:
Results:

Values:

#include [stdafx.h]
#include [my_include.hpp]
#include [sr32.h]
?include (<|\[)+[a-z0-9_].h*(p)+[\]>]
%1exclude [%3>.H%4>]
#exclude [STDAFX.H]
#exclude [MY_INCLUDE.HPP]
#exclude [SR32.H]
%1 – ?
%2 – (<|\[)
%3 - +[a-z0-9_]
%4 – *(p)
%5 – +[\]>]

 


<

Lower Case Operator: This is to be used in conjunction with %n, where %n< replaces the original matched expression with the lower case version. For example,

 

Given:
Search Expression:
Replacement Expression:
Results:

MAKE THIS ALL LOWER CASE
+[A-Z]
%1<
make this all lower case

 


>

Upper Case Operator: This is to be used in conjunction with %n: For example, %1> replaces the original first matched expression with the upper case version. For example,

 

Given the series:
Search Expression:
Replacement Expression:
Results:

All words beginning with letter w.
w+[a-z]
W%1>
All WORDS beginning WITH letter w.

 


%n>>
%n<<

Plain Counter Operator: This is a special case of the %n>#> and %n<#< operators below, where no starting value is defined by the user. When used in conjunction with a numeric regular expression search such as *[0-9], %n>> and %n<< begin incrementing up or down with a value of +/-1 from the value of the first number found by *[0-9]. For example,

 

Given the series:
Search Expression:
Replacement Expression:
Results:

page5.htm, page2.htm, page4.htm
page*[0-9]
page%1>>
page6.htm, page7.htm, page8.htm

 

See Counter Operators for more information & examples.

 


%n>#>
%n<#<

Start Value Counter Operator: An extension of the above, this operator allows you to specify a starting value for an incrementing replacement counter. %n>starting value> and %n<starting value< begin counting up or down with a value of +/-1 from the starting value you supply. This counter operator respects the number of digit places you supply. To begin incrementing upward with a value of 1, use the expression %n>0>. The expression %n>000> would begin replacements with a value of 001. Another example is:

 

Given the series:
Search Expression:
Replacement Expression:
Results:

Var19, Var82, Var8
Var*[0-9]
Var%1<100<
Var99, Var98, Var97

 

See Counter Operators for more information & examples.

 


%n<%num format(Math Op)>

Number Operation: This is a Number Parsing & Math Operation. These operations take search terms that return strings of numbers and handle them internally as integer or decimal numbers (depending on your specifications). Arithmetic operations are optional. If present, they are specified in ( ) before the closing > character.

The format rules are much too complex to summarize here so please see Number Operations for more details.

 

Given:
Search For:
Replace With:
Results:
Explanation:

page5.htm, page2.htm, page18.htm
page*[0-9].htm
page%1<%d(E1-1)>.htm
page4.htm, page1.htm, page17.htm
Subtract 1 from the %1 term using whole integer math.

 


Regular Expression Replacements – Special Characters (Literals)

% \ < >

If you wish to replace any of these characters, they must be preceded by the \ character to be interpreted as a literal in a replacement.

Replacement Operators