我正在尝试使用 Pythonre
模块将字符串中的日期替换为<Month Year>
。
我试过了:
import re
s = "Wikipedia articles containing buzzwords from April 2014t23"
s = re.sub(r"[January|April|March]s+d{1,4}", "<Month Year>", s)
但是,它会返回:
'Wikipedia articles containing buzzwords from Apri<Month Year>t23'
而不是我所期望的:
'Wikipedia articles containing buzzwords from <Month Year>t23'
我哪里出错了?
括号表示其成员(字符(中的替代项,您需要括号。试试这个:
s = re.sub(r"(January|April|March)s+d{1,4}", "<Month Year>", s)
这里的快速示例:
>>> import re
>>> s = "Wikipedia articles containing buzzwords from April 2014t23"
>>> s = re.sub(r"(January|April|March)s+d{1,4}", "<Month Year>", s)
>>> s
'Wikipedia articles containing buzzwords from <Month Year>t23'