Page 1 of 1

regex group dynamic replace

Posted: Thu Apr 27, 2006 3:08 pm
by coreyfurman
I have been writing a program that I use to rename files (in VB6). I can successfully use the regexp object, but I would like to do something with it that I don't see how to accomplish it - specifically, a 'transform' on a backreference, for lack of a better term.

I often work on a group of files that have a sequential number in them for ordering purpose, but sometimes I want to insert a file. I can easily identify the number, but how can I renumber it?

Example:

Code: Select all

Old File        New File
Log02Apr06.txt  Log03Apr06.txt
Log03Apr06.txt  Log04Apr06.txt
Log04Apr06.txt  Log06Apr06.txt
It's easy enough to find what needs to change

regex = ^(Log)(\d\d)(Apr06\.txt)

$2 will contain what I want to transform, but can it be done?

Posted: Thu Apr 27, 2006 4:17 pm
by Chris Corbyn
I don't know much VB but if there's not some built-in callback function for PCRE replacements then I'd probably just do a match and pull out the backreference, then cast it as an integer and post-increment it.

Generally, what a regex does in terms of search and replace, can be done just the same (but slower) using this method.