Regular Expression Number and Math Operators

Search & Replace regex2 shg Math Operators

Number parsing and math replacement operations can be done when a regular expression search term returns a string containing number characters and (optionally) ., -, & + characters. The search term should use an approach similar to Counter Replacements. For example, +[0-9] or +[0-9.\-] can be used in the search term to pick up number strings.

The replacement string syntax is:

%n <[%flags ][width ][.precision ]type (Math Operation )>

The replacement string begins with the %n replacement term to operate on. Following that is the math term, which is enclosed in < > characters. A percent sign – % – must come immediately after the opening < character. The optional + – / * math operation must be enclosed in ( ) characters and must come just before the closing > character. E1, E2,.E2,… are used within the (Math Operation) section to reference the string found by your search term. More detail about this is provided below.

Some sample strings to help you get oriented are: %1<%06ld> and %1<%010.2lf(E1+10)>.

Please see Number Format Overview for more information about the syntax.

Performing + – / * Operations:

To perform a math operation on the found string, specify the format you want and then surround the math syntax inside ( ). Put this just before the closing > character for that component of the replace term.

Use E1, E2, E3, … etc to reference the Search and Replace %n operator to use. For example, if %1 is the found string you want to do a math operation on, use E1. If %3 is the found string of interest, use E3.

It is up to you to make sure the two correspond! The program currently does not check for errors here. You could do something like %1<%d(E4/2)>, which references %1 for the number formatting but then divides %4 by 2. Obviously this would be silly.

General Comments:

  • The syntax can be technical. Plan on experimenting. Use the ‘Display Replace String’ setting in Options|Output to see what the replace would be while doing a search-only pass.

  • Keep good backups while you experiment.

  • Because of a limitation with simple integers, you must use the f-type if the number found is 10 or more digits long.

  • We suggest using literals around your number term search to anchor your search/replaces. If the material of interest is difficult to isolate, consider performing an initial replace to establish some temporary placeholder string(s), then perform your number replacement, then clean up the temporary placer(s). For example, if you search for +[0-9] you will get hits on every number in your file(s). If you can search for, say, +[0-9].tmpjpg, it will be easier to experiment with – and implement – your number operation.

  • Technical users may correctly suspect that the above syntax is derived from Microsoft’s printf and wprintf Functions. Note, however, that not all possibilities apply – or are practical – for Search and Replace. Therefore, please see Number Format Overview for an overview of what is possible with Search and Replace. The n, s, and S data types are not supported by Search and Replace so if you use those they will be ignored. There is some limited support for some printf style type prefixes but in 99% of cases you should not specify anything for a type prefix.

  • Examples

    Replace String



    This replaces the found string of numbers with the same thing, 6 places wide, 0 padded to the left.


    As above but as a floating point number. %4.2lf can set precision.


    Integer arithmetic format followed by a simple arithmetic operation. Note the E1 term. That refers


    Hex format with arithmetic operation


    Given a search term, +[0-9].+[0-9] test, this replace would return the second +[0-9] component of the search hit, plus 10, two decimal places. Note that you could specify %2<%0.2f(E1+10)> but the results will probably be silly because the program would be doing math on the %1 term.


    Similar to the above but this is padded with 0′s.

    +[0-9.\-] bytes



Math Operators