【发布时间】:2021-03-22 21:42:58
【问题描述】:
我正在弄清楚如何获得“02”和“05”或任何其他数字。当数字后面有字母(例如:'a' 和 '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)
它应该只得到下一个数字S 和E。例如,books 2004 必须返回 None。
Thank you all
【问题讨论】:
-
试试
re.findall(r'\d+', title) -
它应该只得到数字'S'和'E'例如'books 2004'必须返回'None'
-
然后使用
try re.findall(r'[SE](\d+)', title)
标签: python python-3.x regex