简体   繁体   中英

How to remove parts of a string that comes after a 'general pattern' using Python

Lets say I have a string like this:

str = 'Friends.S02E05.720p.BluRay.x264.MiniShare.x265'

I want to remove everything after 'S02E05'. This is probably easy. Maybe I can go with something like this:

splitter = 'S02E05'
s1 = str.split(splitter)[0] + splitter      # ==> Friends.S02E05

My problem is, how can I make this code work if the splitter part was 'S03E07'? Basically I'm looking for something to work after 'S E '. What can I do?

Using a regular expression, you could capture the content that you're looking for:

import re


INPUT_STRING = ...
PATTERN = "(.*S\d+E\d+)"

search_obj = re.search(PATTERN, INPUT_STRING)
print(search_obj.group(1))

EDIT: To support S and E upper or lowercase, you could use a flag or just include s and e in the regex:

PATTERN = "(.*[Ss]\d+[Ee]\d+)"

Demo on regex101

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