Regular Expression Number Formatting Overview

Search & Replace regex2 shg Regular Expression Number Formatting Overview

The complete replacement string syntax for a number/math operation 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 number format fields are marked with the ­ arrows. type is the only required field so it marked with blue. The [ and ] characters you see above are for display purposes only. They are not included in actual practice.

The simplest number replacement string that has the minimum required characters is something like: %1<%d> or %1<%f>.

The number format fields are:


This is Required. This character that determines whether to interpret the argument as a character, a string, or a number. See Type Field Characters for the complete details. Examples:

%1<%d> - Given a search term of +[0-9.\-] that returns number strings like +22.5 and -100, d specifies signed decimal integers. Therefore this will output the whole integer.

%1<%f> - Given the same search term, f tells the program to use floating point math. This returns the floating point value, defaulted to 6 places after the decimal.

%1<%+4.2f> - Given the same search term, this outputs a + if the number is +, – if it is -, 4 wide, 2 places after decimal. Note that – is always returned if the value is negative. + simply forces the program to output a literal + as well if the value is positive.

%1<%c> - The c specifies to return the ACSII character that corresponds to the number string found. Given a search term of +[0-9.\-] and a hit on the strings ’67′ and ’98′, the output would be the characters C and b. In the ASCII table, C is character 67 and b is character 98. Of interest, if you want to see an ASCII table, go to the Search and Replace Binary Mode dialog and scroll through the Binary Codes list.


The flags are optional character(s) that control the justification and output of signs, blanks, decimal points, & octal / hexadecimal prefixes. See Flag Characters for details. For Search and Replace, the – flag is meaningless. + is supported. You can pad with spaces or 0′s. Examples:

%1<% 12.3f(E1-10)> - Subtract 10 from the number found by the first search term, output as 12 wide, space padded, 3 places after decimal.

%1<%+06.2f(E1-10)> - Subtract 10, put + in front of positive numbers or – for negative, output at least 6 wide, pad with 0′s.


Width is an optional number that specifies the minimum number of characters to output. In most cases this goes hand in with .precision, although you don’t need to specify .precision. See Width Specification. Examples:

%1<%5d(E1-2)> - Subtract 2 and output a minimum width of 5, using integer math (the d specifies decimal integer).

%1<%5f(E1-2)> - Similar to above but since f is specified, a default of 6 places after the decimal are output.

%1<%5.2f(E1-2)> - This adds a precision spec of .2. Now the output will have 2 places after the decimal.


Precision is an optional number – always preceded by the . character – that specifies the number of characters / characters output. printed for all or part of the output field, or the minimum number of digits printed for integer values. See Precision Values. Examples:

%1<%5.2f(E1-2)> - A precision spec of .2, this outputs 2 places after the decimal.

%1<% 12.6f(E1-2)> - This outputs 6 places after the decimal. The number will be a minimum of 12 wide, padded with <space> if necessary.


Note: This material is taken from the MSDN Run-Time Library Reference, Format Specification Fields: printf and wprintf Functions.

Regular Expression Number Formatting Overview