regex group dynamic replace

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
coreyfurman
Forum Newbie
Posts: 1
Joined: Thu Apr 27, 2006 3:04 pm

regex group dynamic replace

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply