0

I am trying to rename some files but can't wrap my head around the regex pattern needed to do so. I have the following example of a filename:

TV Show Name - 101 - Pilot.jpg

and I want to rename it to:

TV Show Name - 1X01 - Pilot.jpg

This is easy enough, but it gets tricky when I have some filenames like:

TV Show Name - 1001 - Episode.jpg

Which should go to:

TV Show Name - 10X01 - Episode.jpg

The regex pattern I am using to match is:

'.* - [0-9]{3,4} - .*'

What is the best way to rename the files but check if they have 3 or 4 total digits and place the X in the appropriate spot using re.sub?

4
  • 1
    What is the desired output for TV Show Name - 1001 - Episode.jpg? Commented Aug 14, 2018 at 3:04
  • Show how you do the replacement in the easy enough case Commented Aug 14, 2018 at 3:06
  • Sorry, it would be TV Show Name - 10X01 - Episode.jpg Commented Aug 14, 2018 at 3:06
  • @Ajax. You'd have to assume 10x01 Commented Aug 14, 2018 at 3:06

2 Answers 2

3

You can use re.sub:

import re
def new_val(d):
  _d = d.group()
  return _d[:len(_d)-2]+'X'+_d[len(_d)-2:]

s = ['TV Show Name - 101 - Pilot.jpg', 'TV Show Name - 1001 - Episode.jpg']
new_s = [re.sub('(?<=\s\-\s)\d+(?=\s\-\s)', new_val, i) for i in s]

Output:

['TV Show Name - 1X01 - Pilot.jpg', 'TV Show Name - 10X01 - Episode.jpg']
Sign up to request clarification or add additional context in comments.

Comments

2
name = "TV Show Name - 1001 - Episode.jpg"
pattern = re.compile("(.+ - \d+?)(\d{2} - .*)")
parts = pattern.findall(name)
parts[0][0] + 'X' + parts[0][1]
#'TV Show Name - 10X01 - Episode.jpg'

Note: "\d+?" takes as few digits as possible, always leaving exactly two digits for the second part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.