RegEx To Add Zeroes

A swapping-ground for Regular Expression syntax

RegEx To Add Zeroes

Postby Seravox » Sat Oct 11, 2014 1:47 am

I have multiple, large sets of files. Each set is in one of the following formats:

ABC123-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT
ABC1234-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT
ABC12345-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT

The first 3 alphanumerics (ABC) can be any three letters - sometimes capitalized and sometimes not.
The numbers (123, 1234, or 12345) that follow the first 3 alphanumerics can be any 3, 4, or 5 digit number.
The alphanumerics that follow (ABCDEFGHIJKLMNOPQRSTUVWXYZ) the 3, 4, or 5 digit number can be any combination of numbers and letters of variable length.
The file extension is always 3 alphanumerics and can contain 3 letters or 2 letters and a number.

If someone would be so kind, I need a RegEx that will convert the 3 digit and 4 digit numeric to a 5 digit numeric by adding the appropriate 2 or 1 zeroes, respectively. So, ABC123-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT would become ABC00123-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT and ABC1234-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT would become ABC01234-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT.

Many thanks!

P.S. I tried to figure it out and after an hour, I was as lost as when I started. Ha.
Seravox
 
Posts: 1
Joined: Sat Oct 11, 2014 1:24 am

Add Zeroes to pad a existing number

Postby Stefan » Sat Oct 11, 2014 11:28 am

 
An better overview for easier developing an solution:

FROM:
ABC123-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT
ABC1234-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT
ABC12345-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT

TO:
ABC00123-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT
ABC01234-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT.
ABC12345-ABCDEFGHIJKLMNOPQRSTUVWXYZ.EXT

RULE:
On first thought: get every number after first three chars till the underscore. Be sure that that part will have a length of five numbers, by adding leading zeros.

USE:
?
To be developed.... something like ([a-z]{3})(\d+)(-.+)


- - -


Hmm, i think we will ending up in doing two search & renames cycles:

1.) change ABC<three digits> names:
RegEx(1)
Find: ([a-z]{3})(\d\d\d)(-.+)
Replace: \100\2\3


2.) change ABC<four digits> names
RegEx(1)
Find: ([a-z]{3})(\d\d\d\d)(-.+)
Replace: \10\2\3
 



- - -

Or in one step

http://ghisler.ch/board/viewtopic.php?p=289370#289370

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


Return to Regular Expressions