Page 1 of 1

Adding dash in different positions

PostPosted: Fri Aug 19, 2022 2:02 pm
by Otto
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!

Insert dash inbetween letters and the first numbers

PostPosted: Fri Aug 19, 2022 2:21 pm
by Luuk
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

Re: Insert dash inbetween letters and the first numbers

PostPosted: Fri Aug 19, 2022 2:43 pm
by Otto
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?

Re: Adding dash in different positions

PostPosted: Fri Aug 19, 2022 4:38 pm
by Luuk
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]?)$

Re: Adding dash in different positions

PostPosted: Fri Aug 19, 2022 5:52 pm
by Otto
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