多正则表达式以获取 python 中的数字

multi regex to grab numbers in python

我不是正则表达式专家,但我是从头开始做的 基于此,我只需要获取数字和字母 [a-e] :

SnnEnn   where n represent numbers  
SnnEnnl  where l = letter [a-e]  

nnXnn    where n represent numbers 
nnXnnl   where l = letter [a-e] 

但在少数情况下会失败:

02x01      match '02x01'                                      fail (It should be '02' '01')
03x02a     match '03x02'..............groups 'a'             fail (It should be '03' '02' 'a')
S03E01     match 'S03E01'.............groups '03'  '01'      ok
S03E01a    match 'S03E01a'............groups 'S03E01'  'a'   fail.(It should be '03' '01' 'a')

03x02xxxx  match '03X02xxxx' groups '03x02xxxx'    fail (it should be just [a-e] and limit to one letter) 
S03E01xxx  match '03E01xxxx' groups '03e01xxxx'    fail (idem)

regex101

我正在使用这个正则表达式:

(\d+[x]\d+\w)([a-e])|(\d+[x]\w+)|\bS(?P<Season>\d+)E(?P<Episode>\d+)|(\b[s]\d+[e]\d+)([a-e])

感谢您的帮助

您可能会使用

\b(S)?(?P<Season>\d+)(?(1)E|x)(?P<Episode>\d+)(?P<Letter>[a-e]?)\b
  • \b一个单词边界
  • (S)? 可选择在组 1
  • 中捕获 S
  • (?P<Season>\d+)Season - 匹配 1+ 个数字
  • (? 条件(参见 this page about conditionals
    • (1)E 如果组 1 存在,匹配 E
    • | 其他
    • x 匹配 x
  • ) 关闭条件
  • (?P<Episode>\d+)Episode - 匹配 1+ 个数字
  • (?P<Letter>[a-e]?)Letter - 匹配范围a-e
  • \b一个单词边界

Regex demo