我一直在努力获得一个正则表达式来获取电视节目或电影名称,如果它存在的话,它被播出的那一年,从视频的文件名开始的第一季#和第一集#。我有一个正则表达式(如下所示),这个表达式在电影和电视节目中都适用于双年度日期的节目(其中一年在放映/电影名称中,另一年是播出的年份)。对于电视节目,如果格式为SXXEXX或XXX,则可以获取季数和集号。我一直在regex101.com测试引擎中测试它。我正在挣扎的地方是,如果文件名中不存在一年,表达式将不会返回任何内容。此外,如果文件名有一个4位数的数字,实际上是节目名称的一部分,它认为这是播出的年份日期(即“4400")。如何修改这个表达式以处理我所描述的额外条件?
最终的目标是,我想把它放到一个python脚本中,如果这个文件是电影或电视节目,它会查询像TheTVDB.com这样的站点,这样我就可以将我庞大的视频库分类到TV show和电影文件夹中。
(?P<ShowName>.*)[ (_.]#Show Name
(?=19[0-9]\d|20[0-4]\d|2050) #If after the show name is a year
(?P<ShowYear>\d{4,4}) # Get the show year
| # Else
(?=S\d{1,2}E\d{1,2})
S(?P<Season>\d{1,2})E(?P<Episode>\d{1,2}) #Get the season and Episode information
|
(\d{1})E(\d{1,2})这是我正在使用的测试数据
正则表达式不能正常工作于下列测试数据:
更新:以下是基于注释的新表达式。它的工作方式要好得多,但它很难处理表达式下面列出的3个文件名。
(?P<ShowName>.*)#Show Name
(
[ (_.]
(
(?=\d{4,4}) #If after the show name is a year
(?P<ShowYear>\d{4}) # Get the show year
| # Else no year in the file name then just grab the name
(?P<otherShowName>.*) # Grab Show Name
(?=S\d{1,2}E\d{1,2}) # If the Season Episode patterns matches SX{1,2}EX{1,2}, Then
S(?P<Season>\d{1,2})E(?P<Episode>\d{1,2}) #Get the season and Episode information
| # Else
(?P<Alt_S_E>\d{3,4}) # Get the season and Episode that looks like 211
)
|$)发布于 2014-09-12 13:14:59
我对你的regex做了一些修改,如果我正确理解你的话,这似乎是可行的。
^(
(?P<ShowNameA>.*[^ (_.]) # Show name
[ (_.]+
( # Year with possible Season and Episode
(?P<ShowYearA>\d{4})
([ (_.]+S(?P<SeasonA>\d{1,2})E(?P<EpisodeA>\d{1,2}))?
| # Season and Episode only
(?<!\d{4}[ (_.])
S(?P<SeasonB>\d{1,2})E(?P<EpisodeB>\d{1,2})
| # Alternate format for episode
(?P<EpisodeC>\d{3})
)
|
# Show name with no other information
(?P<ShowNameB>.+)
)参见regex101演示
编辑:,我已经更新了regex,以处理您在评论中提到的最后3种情况。
一个主要的问题是,在主替换的周围没有父母亲,所以它包含了整个regex。我还必须添加一个替换,以便不允许名称后面的年份/插曲格式。
因为您有很多不同的可能布局,可能会相互冲突,所以regex最终会出现许多不同场景的变化。例如,为了匹配一个根本没有年份或插曲信息的标题,我必须在整个regex周围添加一个替换,如果它找不到任何已知的模式,只需匹配整个内容。
注意:现在您似乎已经扩展了显示年以匹配任何四位数,因此不需要向前看。换句话说,(?=\d{4,4})(?P<ShowYear>\d{4})和(?P<ShowYear>\d{4})是一样的。这也意味着你的插集的替代格式必须只匹配3位数字,而不是4位。否则,就无法将独立的4位数字序列区分为一年或一集。
一般模式:
[ (_.]+ the delimiter used throughout
(?P<ShowNameA>.*[^ (_.]) the show name, greedy but not including a delimiter
(?P<ShowNameB>.+) the show name when it's the whole line格式A(有可能的季节和史诗的年份):
(?P<ShowYearA>\d{4})
([ (_.]+S(?P<SeasonA>\d{1,2})E(?P<EpisodeA>\d{1,2}))?格式B(仅限季刊和第一集):
(?<!\d{4}[ (_.])
S(?P<SeasonB>\d{1,2})E(?P<EpisodeB>\d{1,2})格式C(插曲的替代格式):
(?P<EpisodeC>\d{3})发布于 2015-03-31 16:56:52
如果可以的话,我调整了brian的正则表达式来匹配
SHOW.NAME.201X.SXXEXX.XSUB.VOSTFR.720p.HDTV.x264-ADDiCTiON.mkv
这就是( PCRE)
/^(
(?P<ShowNameA>.*[^ (_.]) # Show name
[ (_.]+
( # Year with possible Season and Episode
(?P<ShowYearA>\d{4})
([ (_.]+S(?P<SeasonA>\d{1,2})E(?P<EpisodeA>\d{1,2}))?
| # Season and Episode only
(?<!\d{4}[ (_.])
S(?P<SeasonB>\d{1,2})E(?P<EpisodeB>\d{1,2})
)
|
# Show name with no other information
(?P<ShowNameB>.+)
)/mxhttps://stackoverflow.com/questions/25807795
复制相似问题