Regular Expression Number and Math Operators

image\regex2_shg.gif

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: