Search code examples
regex

RegexRenamer, how to add leading zeros?


I have hundreds of files with names of type "xx S1E1 y1", "xx S2E15 y2", "xx S3E100 y3".

With RegexRenamer I'm trying to add leading zeros after E, but I haven't been able to do so, it always put the leading zeros before E.

(I need E1 to become E001, E10 to E010 and do nothing with E100.)

E.g. for those file names that are "xx S1E[1-9] xx", I'm trying:

Match: (E\d{1}) Replace: 0$0

Which causes S1E1 to become S10E1 instead of S1E01.

Anyone knows how to do this properly with RegexRenamer?


Solution

  • Try this regex

    (?<=E)((?=\d(?!\d))|\d(?!\d\d))
    

    And replace with

    0$0
    

    Not sure if RegexRenamer supports conditional substitution, but here's a workaround. The trick is when there's only one digit after E, it matches twice (zero-width character after E, and the following digit). So it could insert two 0s.

    When there are two digits after E, it only matches once (the digit after it), and only one 0 is inserted.

    And if there are more than 2 digits, it matches nothing.

    See test cases