简体   繁体   中英

Regex pattern replacing in Python

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 ?

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']
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM