我尝试了以下内容:
title = 'Die.Simpsons.S02.German'
season = re.compile('.*S\d|\Sd{2}|eason\d|eason\d{2}.*')
test = season.match(title)
print test
但我总是收到' none'
答案 0 :(得分:3)
根据您的变量名称,我假设您对季节编号感兴趣,而不是整个标题。如果我是对的,它应该是这样的:
title = 'Die.Simpsons.S02.German'
# This will match Die.Simpsons.S1, Die.Simpsons.S01, Die.Simpsons.Season1 etc ...
reg = re.compile('.*(S|Season|eason)(\d+)')
# get only the season number, group(0) gives full match, group(1) first '()' and so on
season = reg.match(title).group(2)
print season # prints '2'
您可以使用reg.match
代替reg.search
,而不需要在开头.*
:
reg = re.compile('(S|Season|eason)(\d+)')
season = reg.search(title).group(2)
// EDIT 托马斯评论后修正
答案 1 :(得分:0)
使用此代码可以:
import re
regex = r".*S(eason)?\d{1,2}.*"
test_str = "Die.Simpsons.S02.German"
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found : {match}".format(matchNum = matchNum, match = match.group()))
请参阅demo。