Adding dash in different positions

Bulk Rename Utility How-To's

Adding dash in different positions

Postby Otto » Fri Aug 19, 2022 2:02 pm

Hello, guys, how are you doing? I wonder if you can help me with this:
I have many files like this:
ab1234a
abc123a
abcd123a
abcde123a

I need to add a hyphen between the first letters and numbers, so I could have:
ab-1234a
abc-123a
abcd-123a
abcde-123a

The hyphen could be the 3rd, 4th, 5th or 6th character, always like [letter] hyphen [number]. Note that I don't want hyphen between the numbers and the last letter.

I'm having a hard time to figure this out.
Could you give me a help with this? Thanks!
Otto
 
Posts: 3
Joined: Fri Aug 19, 2022 1:35 pm

Insert dash inbetween letters and the first numbers

Postby Luuk » Fri Aug 19, 2022 2:21 pm

If you're being very exact with the samples, then RegEx(1) can use a "Match" and "Replace" like...

^([A-Za-z]{2,5})(\d{3,4}a)$
\1-\2
Luuk
 
Posts: 691
Joined: Fri Feb 21, 2020 10:58 pm

Re: Insert dash inbetween letters and the first numbers

Postby Otto » Fri Aug 19, 2022 2:43 pm

Luuk wrote:If you're being very exact with the samples, then RegEx(1) can use a "Match" and "Replace" like...

^([A-Za-z]{2,5})(\d{3,4}a)$
\1-\2


Thank you very much! :D
I wasn't very exact. Sometimes, the last character is not present and when it's present, it could be any letter, like a, b, c, d...
abc123
abc123a
abc123b

So when the last letter is not present, this works:
^([A-Za-z]{2,5})(\d{3,4})$

When the last letter is present, this works for any letter:
^([A-Za-z]{2,5})(\d{3,4}[a-z])$

I could do the renaming in two steps, but is there a way to put both in the same formula?
Otto
 
Posts: 3
Joined: Fri Aug 19, 2022 1:35 pm

Re: Adding dash in different positions

Postby Luuk » Fri Aug 19, 2022 4:38 pm

Just put a ? after the last [a-z].. Its just a shortcut to say {0,1} so then it can match either none -or- 1...
^([A-Za-z]{2,5})(\d{3,4}[a-z]?)$
Luuk
 
Posts: 691
Joined: Fri Feb 21, 2020 10:58 pm

Re: Adding dash in different positions

Postby Otto » Fri Aug 19, 2022 5:52 pm

Luuk wrote:Just put a ? after the last [a-z].. Its just a shortcut to say {0,1} so then it can match either none -or- 1...
^([A-Za-z]{2,5})(\d{3,4}[a-z]?)$


It worked beautifully!
I can't thank you enough! :D
Otto
 
Posts: 3
Joined: Fri Aug 19, 2022 1:35 pm


Return to How-To