python中的Regex数字和字母



我正在计算如何获得'02'和'05'或任何其他数字。当数字后面有字母时(例如:"a"one_answers"b"或任何其他字母(

title = "Nursing informatics S02E05ab Jack"       ->02 and 05
title = "Medical diagnosis   S06E06ku Peter"      ->06 and 06
title = "medical protection  S01E02bc Katharina"  ->01 and 02

我试过这样做,但它总是返回"无">

result = re.search(r"b(?:e?)?s*(d{2,3})(?:[a-z]?)?b", title, re.IGNORECASE)

它应该只得到下一个SE的编号。例如,books 2004必须返回None

Thank you all

下面的正则表达式函数(findall(可以识别所有指定的模式:

import re
s = "Nursing informatics S02E05ab Jack"
re.findall('[0-9]+', s)

输出:

['02', '05']

您可以使用

bS(?P<Season>d+)E(?P<Episode>d+)

请参阅regex演示详细信息

  • b-一个词的边界
  • S-字母S
  • CCD_ 8-组";季节":一个或多个数字
  • E-一个E字母
  • CCD_ 11-组";情节":一个或多个数字

请参阅Python演示:

import re
title = "Nursing informatics S02E05ab Jack" 
m = re.search(r'bS(?P<Season>d+)E(?P<Episode>d+)', title)
if m:
print( m.groupdict() )
# => {'Season': '02', 'Episode': '05'}

最新更新