Leading zeros

A swapping-ground for Regular Expression syntax

Leading zeros

Postby saintoctopus » Tue Sep 17, 2019 1:21 pm

Hello - I am new to RegEx but it should do what I want and I'm hoping someone lovely will be able to assist! I can't find anything on the interwebs which provides a comprehensive solution, so I'm turning to you...

I have read https://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=453 which sounds like it should work, and it does to a degree

The expression from above is:
Search: (.+\D\s*)(\d)$
Replace: \10\2

Basically, I have thousands of files with names which follow no real naming convention. I want to find any SINGLE DIGIT NUMBER and add a 0 to the start,so that when the files are listed (in SharePoint) they list correctly.

So currently:
1
2
...
10

Lists as:
1
10
2
...

With leading zeros, it'll be:
01
02
...
10

The code above works, BUT if there is anything after the single digit, it fails.
e.g.
Lesson 1 - becomes Lesson 01
Lesson 1 DNA - doesn't change


You mission: To improve the code to catch all single digits anywhere in a filename?
saintoctopus
 
Posts: 3
Joined: Tue Sep 17, 2019 1:04 pm

Re: Leading zeros

Postby therube » Tue Sep 17, 2019 9:21 pm

This will find (the first) "interior" single digit.
It will not find single digits at the start or at the end (though you can certainly search for those).

1:RegEx:
Code: Select all
Match:  (.*?)( )(\d)( )(.*)
Replace:  \1 0\3 \5

Sample file names:
Code: Select all
0 123  2  abc  3  456 7.TXT
1 123  2  abc  3  456 7.TXT

In this case, there are 2 interior single digits (2 & 3).
So you'd have to run the rename twice (or as many times until there are no more changes to be made), consecutively - after selecting/deselecting the files (so that it picks up the changes).

First time through it finds " 2 " & renames it to " 02 ".
Deselect/Select.
Second time though, it finds " 3 " & renames it to " 03 ".

At that point this sample is done, no further matches.
If there were more, simply repeat.


Match everything, non-greedily, up to <sp>digit<sp>.
Match the single digit.
Match everything that follows.


Likewise, you could set up rules to pick up the leading (0 , 1 ) & ending ( 7), if they are a consideration.
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Leading zeros

Postby saintoctopus » Wed Sep 18, 2019 12:00 pm

therube wrote:1:RegEx:
Code: Select all
Match:  (.*?)( )(\d)( )(.*)
Replace:  \1 0\3 \5



Oh my word, that's brilliant - thank you!

So, what could I then do about a number at the start or the end?
saintoctopus
 
Posts: 3
Joined: Tue Sep 17, 2019 1:04 pm

Re: Leading zeros

Postby therube » Wed Sep 18, 2019 2:26 pm

Start
Code: Select all
Match:  ^(\d\s)(.*)
Replace:  0\1\2


End
Code: Select all
Match:  (.*)(\s\d)$
Replace:  \10\2


Now, as written, they would not match something like:
0.ARJ
(would not end up as 00.ARJ, as there is no whitespace)
or
1. Himekami - Tsugaru - vi Kaze-no-Komoriuta.mp3
(the . is not whitespace & there could be cases where you'd want the song name renamed with a leading 0, 01. Himekami - Tsugaru - vi Kaze-no-Komoriuta.mp3)
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Leading zeros

Postby saintoctopus » Fri Sep 20, 2019 3:29 pm

Thank you so much for your help. For the record, in case someone else is looking, the code I ended up using is as follows:

To find single digits at the start of the filename and adding a leading zero:

Code: Select all
Match:    ^(\d\D)(.*)
Replace:  0\1\2


To find single digits in the middle of a filename regardless of what comes before or after, and adding a leading zero:

Code: Select all
Match:    (.*?)(\D)(\d)(\D)(.*)
Replace:  \1\20\3\4\5


To find single digits at the end of a filename and adding a leading zero:

Code: Select all
Match:    (.*)(\D)(\d)$
Replace:  \1\20\3


Worked for me with one or two matches which were inappropriate, but that can't be helped.
saintoctopus
 
Posts: 3
Joined: Tue Sep 17, 2019 1:04 pm


Return to Regular Expressions