Only target a letter after a string of numbers

A swapping-ground for Regular Expression syntax

Only target a letter after a string of numbers

Postby garblojones » Sun Mar 29, 2020 10:55 pm

Hello all - i apologize if this has already been asked in another form but I tried searching a few different ways and couldn't come up with any results specifically to what I'm looking for.

My request is pretty simple but i'm not familiar at all with RegEx and I'm hoping this isn't too complex.

I have a few hundred files that all start with a person's name, an entry number and a following A, B (sometimes C or D) and they were labeled pretty inconsistently to begin with. That being said, all I want to do is target that last A, B, or C after a string of numbers (3 digits) and change the character (if it exists) to -Section1, -Section2 etc... but some of them don't have a letter at the end and I only want to change that last letter (if it exists) to a "Section1" or "Section2" respective to the A, B, C ... )

An example of a few different file names:
JohnP-001.doc
JohnP-002A.doc
JohnP-002B.doc
Sara-C-Paper010.doc
Sara-C-Paper011A.doc
Sara-C-Paper011B.doc

And I want to rename them as follows:
JohnP-001.doc
JohnP-002-Section1.doc
JohnP-002-Section2.doc
Sara-C-Paper010.doc
Sara-C-Paper011-Section1.doc
Sara-C-Paper011-Section2.doc

Thank you in advance for your help!
garblojones
 
Posts: 2
Joined: Sun Mar 29, 2020 10:44 pm

Re: Only target a letter after a string of numbers

Postby RegexNinja » Mon Mar 30, 2020 11:42 am

Unfortunately, regex doesnt allow conditionals in the replacement, but you can do something like:

#1 Regex Match/Replace:
^(.*-.*\d{3})([A-D])$
\1|\2

#13 CharTranslation:
|,A=-,S,e,c,t,i,o,n,1
|,B=-,S,e,c,t,i,o,n,2
|,C=-,S,e,c,t,i,o,n,3
|,D=-,S,e,c,t,i,o,n,4

Since regex processes before CharTrans, it inserts | before the final Uppercase [A-D].
This lets CharsTrans only process files that regex 'tagged' with | (an illegal filename-char).

With many files, you can limit the file-selection with a #12Filter: ^(.*-.*\d{3})([A-D])\.doc$
Then underneath: Check the 12Filter RegEx-box, and use F5/Refresh to narrow the selection.

Results:
Original Names------> Regex Intermediate --> CharTrans Final Result
JohnP-002A ---------> JohnP-002|A -----------> JohnP-002-Section1
Sara-C-Paper011B --> Sara-C-Paper011|B ---> Sara-C-Paper011-Section2
JohnP-001 -----------> no effect
Sara-C-Paper010 ----> no effect
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Only target a letter after a string of numbers

Postby therube » Mon Mar 30, 2020 1:14 pm

(Not the complete RegEx...)
Code: Select all
Match:  (\d\d\d)([ABCD])
Replace:  \1-Section\2

You could hardcode the -Section part in the Replace:

Then you could change your Character Translation to simply:
Code: Select all
A=1
B=2
C=3
D=4


(Just makes it a little "cleaner".)

Ah, that's not going to work!

(Not the complete RegEx...)

I should have finished it before I posted it.

Worked well - in theory ;-).

But... Character Translation works globally.
So the 'C' in Sara-C also gets translated (to 3) - which is wrong.
Code: Select all
Sara-C-Paper011A.doc -> Sara-3-Paper011-Section1.doc


So, like above, add a "throw-away" (unique, if you will) character into the mix (the pipe, |), & that gives you something "unique" to Character Translate against, so the 'C' in Sara-C is left unaffected.
therube
 
Posts: 889
Joined: Mon Jan 18, 2016 6:23 pm

Re: Only target a letter after a string of numbers

Postby Admin » Tue Mar 31, 2020 12:00 am

In Javacript you could have a script similar to this:

- if last char of name is A, replace with -Section1
- if last char of name is B, replace with -Section2
- if last char of name is C, replace with -Section3

and so on...
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Only target a letter after a string of numbers

Postby RegexNinja » Wed Apr 01, 2020 6:36 pm

Thanks again, Admin!!.. Here's another way to do it, javascript style:

newName=name.substr(-4).match(/\d{3}[A-D]/)
if (newName.endsWith('A')) newName= name.slice(0,-1) + '-Section1'
else if (newName.endsWith('B')) newName= name.slice(0,-1) + '-Section2'
else if (newName.endsWith('C')) newName= name.slice(0,-1) + '-Section3'
else if (newName.endsWith('D')) newName= name.slice(0,-1) + '-Section4'

Results: Identical to the Regex/CharTrans method posted above.
Cheers!
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Only target a letter after a string of numbers

Postby garblojones » Wed Apr 01, 2020 8:50 pm

Thanks everyone for your input! (pun intended) - since I made the OP, I took it upon myself to familiarize myself with RegEx and I have to say - now that I've seen a few tutorials and done a bit of practice, I feel leaps and bounds more comfortable thinking in terms of it. I still ended up having to do it in a couple of steps, but a lot easier than going through and doing them individually:

Example filenames
JohnP-001.doc
JohnP-002A.doc
JohnP-002B.doc
Sara-C-Paper010.doc
Sara-C-Paper011A.doc
Sara-C-Paper011B.doc

MATCH
(.*)(\d\d\d)([A-D])
matches every file that has A-D at the end

REPLACE
\1 - \2 Section-\3
added the space hyphen space between name and 3 digit number (just so it looks cleaner) and the "Section-" before the alpha character.
Then I just went and changed all the "Section-A" to "Section-1" and so on since there were only 4 variables, it only took a minute to get through all of them.

RegexNinja wrote:Thanks again, Admin!!.. Here's another way to do it, javascript style:

newName=name.substr(-4).match(/\d{3}[A-D]/)
if (newName.endsWith('A')) newName= name.slice(0,-1) + '-Section1'
else if (newName.endsWith('B')) newName= name.slice(0,-1) + '-Section2'
else if (newName.endsWith('C')) newName= name.slice(0,-1) + '-Section3'
else if (newName.endsWith('D')) newName= name.slice(0,-1) + '-Section4'

Results: Identical to the Regex/CharTrans method posted above.
Cheers!


Though now I'm super interested in exploring this one-swing approach, but I'm not a coder by any means so I would've never been able to even imagine this lol.

Thanks again everyone!
garblojones
 
Posts: 2
Joined: Sun Mar 29, 2020 10:44 pm

Re: Only target a letter after a string of numbers

Postby RegexNinja » Wed Apr 01, 2020 10:54 pm

So glad you worked it out.. Im just now (slowly) learning some of this javascript stuff..
Its definitely got a lot of advantages over standard regex, there's a good manual over at http://www.bulkrenameutility.co.uk/foru ... =12&t=4743
Theres a new one coming soon, I think its gonna have alot more regex/javascript examples..

Btw, the Regex/CharTran method is also one-swing approach, but you have to use them both.
Each one was designed to fail without the other, lol.
Cheers!
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm


Return to Regular Expressions