Regular Expression Incrementing Counters

Search & Replace regex2 shg Regular Expression Counters

Search & Replace relicon1 Regular Expression Counters Also see

Regular Expression Counter operations allow you to revise a sequence of numbers in one or more files. The program can also insert sequential numbers in strings where no numbers exist originally. Counter operations use the ?[0-9], +[0-9], or *[0-9] regular expression search operators and the %n>user defined starting value> or %n<user defined starting value< regular expression replacement operators. Counter increment +/-1 from the starting value specified. If no value is supplied, as in %n>> or %n<<, then incrementing begins with a value of +/- 1 from the value found by your *[0-9] expression. For example, if the value found by the *[0-9] search expression is 10, %n>> begins at 11.

The %n>user defined starting value> and %n<user defined starting value< replacement operators respect the number of digit places supplied. For example, the expression %1>1> begins incrementing with 2. The expression %1>000> begins incrementing with 001.

Negative values can be used for the starting value. For example, %1>-5> replaces with -4, -3, -2, -1, 0, … .

Arithmetic calculations are not available for counter operations. The?[0-9], +[0-9], or *[0-9] operators do not perform numeric evaluations. These are ordinary [ ] range operators that search for the characters 0 to 9. Keep this in mind if you are searching for number strings along with $ or . characters. A search for +[\$.0-9] finds ‘$53.00′ but the program does not interpret this as ‘fifty-three dollars’. Counters will not work in this case. The – (minus) character is an exception. +[\-0-9] is OK for counter operations involving search hits such as ‘-53′. If you need to do searching-counters for strings like ‘$53.00′, consider a search string such as \$+[0-9].+[0-9] and (perhaps) a replace such as $%1>>.%2.

Incrementing counter operations may be combined with other regular expression search & replace operators. For example, a search expression such as (file|variable)*[0-9] with a counter replacement expression such as %1%2>100> is allowed.

If the search uses multiple?[0-9], +[0-9], or *[0-9] search terms the replacement can use different counter terms. Example: Search – cat*[0-9] dog*[0-9], Replace – cat%1>> dog%2>100>. Please note that the replacement term should maintain the positions of the %n terms in the search string. Do not, for example, use cat%2>> dog%1>100> in a replace string.

By default %n>user defined starting value> & %n<user defined starting value< reset with each file if your search spans multiple files. See the “Multiple Files – Default Behavior” example below.

A special registry switch can be used to cause %n>user defined starting value> & %n<user defined starting value< replacements to sequence across files. The special configuration utility on the Search & Replace home page can also be used to activate the “Count Across Files” switch. The switch can also be used in manually created scripts.

See the  ”Multiple Files – Sequence Across Files” below for examples of the behavior when the program sequences across files. The order in which the files are processed is determined by the sort order of files in the Search Results list. This can be adjusted via the ‘Sort search results by’ field in Options-Search. For example, given FILE1.TXT and FILE2.TXT, a sort order of File Name-Ascending causes FILE1.TXT to begin the sequence. A sort order of File Name-Descending causes FILE2.TXT to begin the sequence.

Regular Expression Counter Examples

Initial Contents:
Search String:
Replace String:
Results:

Windows 98 will be released in 5 days.
*[0-9]
%1>>
Windows 99 will be released 100 days.

Initial Contents:
Search String:
Replace String:
Results:

file.htm, file.htm, ffillee.htm
e*[0-9].htm
e%1>>.htm
file2.htm, file3.htm, ffillee4.htm

Initial Contents:
Search String:
Replace String:
Results:

Var22 Var20 Var86 Var30
Var*[0-9]
Var%1<50<
Var49 Var48 Var47 Var46

Initial Contents:
Search String:
Replace String:
Results:

Var22 Var20 Var86 Var30
Var*[0-9]
Var%1>00>
Var01 Var02 Var03 Var04

Initial Contents:
Search String:
Re
place String:
Results:

VarA101 VarB12 VarC0 VarA102 VarB45
Var[a-z]*[0-9]
Var%1%2>08>
VarA09 VarB10 VarC11 VarA12 VarB13

 

Regular Expression Counter Example
Multiple Files – Default Behavior

File1 Contents:
File2 Contents:
Search String:
Replace String:
File1 Results:
File2 Results:

cat01 dog01
cat20 dog15
cat*[0-9] dog*[0-9]
cat%1>01> dog%2>04>
cat01 dog05
cat01 dog05

 

Regular Expression Counter Example
Multiple Files – Sequence Across Files

File1 Contents:
File2 Contents:
Search String:
Replace String:

Sort Order:
File1 Results:
File2 Results:

Sort Order:
File2 Results:
File1 Results:

cat01 dog01
cat01 dog01
cat*[0-9] dog*[0-9]
cat%1>009> dog%2>019>

File Name-Ascending
cat010 dog020
cat011 dog021

File Name-Descending
cat011 dog021
cat010 dog020

   

Regular Expression Counters