Need regex help, matching captures with replaces

Post any Bulk Rename Utility support requirements here. Open to all registered users.

Need regex help, matching captures with replaces

Postby wessin » Wed Apr 29, 2015 9:50 pm

I'm trying to figure out how to use regex to replace several possible strings in filenames and am not even sure it's possible to do so. Here's what I have:

A filename with a string of text and a number 1.jpg
A filename with a string of text and a number 1 and more text.jpg
A filename with a string of text and a number 17.jpg
A filename with a string of text and a number 17 and more text.jpg
A filename with a string of text and a number 105.jpg
A filename with a string of text and a number 105 and more text.jpg

I would like grab everything prior to the number and replace it. Easy enough, I can do this with no problem.
I would like to prefix 0s to each number that is less than 3 digits. For example, a 1-digit number should have '00' prefixed. A 2-digit number should have '0' prefixed. A 3-digit number should not have anything prefixed. Final result is that every number is 3-digits.
I would like to preserve everything after the number.

So the final result should be:

New replacement text 001.jpg
New replacement text 001 and more text.jpg
New replacement text 017.jpg
New replacement text 017 and more text.jpg
New replacement text 105.jpg
New replacement text 105 and more text.jpg

I've been working so far with structures similar to:
(.*) ((\d{3})(.*)|(\d{2})(.*)|(\d{1})(.*))

but what I don't know is how to replace group to group, or if it's even possible. For example:
(\d{3}) would be replaced by \2
(\d{2}) would be replaced by 0\2
(\d{1}) would be replaced by 00\2

I'm starting to think this is not possible in a single step. Can anyone help out here?
wessin
 
Posts: 9
Joined: Wed May 09, 2012 9:54 pm

Pad digits with zeros to specific length

Postby Stefan » Thu Apr 30, 2015 7:58 am

Split your work into several parts. Example:


Rule: you have only ONE single digit or group of digits in your file name.
Step1: modify the digit(s) by adding a lot of zeros, to all of them!
Step2: match the digits again, but keep only the last three, drop the leading superfluous zeros.

--------------------

Step 1 - Add zeros:

RegEx(1)
Match: (.+ )(\d+)(.*)
Repla: \100\2\3

New Name:
A filename with a string of text and a number 001 and more text.jpg
A filename with a string of text and a number 001.jpg
A filename with a string of text and a number 00105 and more text.jpg
A filename with a string of text and a number 00105.jpg
A filename with a string of text and a number 0017 and more text.jpg
A filename with a string of text and a number 0017.jpg
[Rename]

--------------------

Step 2 - remove superfluous zeros:

RegEx(1)
Match: (.+ )(0*)(\d\d\d)(.*)
Repla: \1\3\4

New Name:
A filename with a string of text and a number 001 and more text.jpg
A filename with a string of text and a number 001.jpg
A filename with a string of text and a number 105 and more text.jpg
A filename with a string of text and a number 105.jpg
A filename with a string of text and a number 017 and more text.jpg
A filename with a string of text and a number 017.jpg
[Rename]

--------------------

Step 3:

Do your other work...



--------------------


btw, a more "pro" expression would be,

for step 1:
Match: (.+ )(\d+.*)
Repla: \100\2
(or even: \1000000000\2 will work too)

for step 2:
Match: (.+ )0*(\d{3}.*)
Repla: \1\2

But my first one should be better for easy understanding.


 
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Need regex help, matching captures with replaces

Postby wessin » Mon May 04, 2015 8:06 pm

Thanks, this worked perfectly.
wessin
 
Posts: 9
Joined: Wed May 09, 2012 9:54 pm


Return to BRU Support