捕获电影标题组

2024-10-03 04:25:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从电影标题中捕捉以下组:

file = "The Great Home Se01E01 Meatballs for Dinner"

<show> = "The Great Home"
<season> = "Se01"
<episode> = "E01"
<title> = "Meatballs for Dinner"

目前,我只能部分地捕获并使用以下代码:

import re

file = "The Great Home Se01E01 Meatballs for Dinner"
seasonEpID = re.search(r'(\bS/?.+\d{1,2})+(E/?.+\d{1,2})', file)
print(seasonEpID.groups())

返回以下内容:

('Se01', 'E01')

如何捕捉这四个组<show><season><episode><title>


Tags: therehomefortitleshowfileseason
2条回答

我将re.findall与以下正则表达式模式一起使用:

^(.*?)\s+(Se\d+)(E\d+)\s+(.*)$

示例脚本:

file = "The Great Home Se01E01 Meatballs for Dinner"
parts = re.findall(r'^(.*?)\s+(Se\d+)(E\d+)\s+(.*)$', file)
print(parts)

这张照片是:

[('The Great Home', 'Se01', 'E01', 'Meatballs for Dinner')]
import re
file = "The Great Home Se01E0k1 Meatballs for Dinner"
match = re.fullmatch(r"(?P<show>.+?) (?P<season>Se\d+)(?P<episode>E\d+) (?P<title>.+)", file)
print(match.groupdict() if match else "No match") 

'''
{
  'episode': 'E01',
  'season': 'Se01',
  'show': 'The Great Home',
  'title': 'Meatballs for Dinner'
}
'''

相关问题 更多 >