【发布时间】:2018-12-04 07:13:15
【问题描述】:
我正在尝试将两个列表替换为文本:
text = "today is friday july 1 2018"
days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
daysRegex = re.compile('|'.join(map(re.escape, days)))
months = ['january', 'february', 'march', 'april', 'may', 'mai', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
monthsRegex = re.compile('|'.join(map(re.escape, months)))
replaces = daysRegex.sub("<day>", text) and monthsRegex.sub("<month>", text)
print(replaces)
输出:
今天是星期五 1 2018
正确的输出:
今天是
1 2018
我不确定我是否正确使用了 and 运算符。我只是想把我研究的东西付诸实践(但我可能误解了)
【问题讨论】:
-
你见过这个关于'and'和'or'的问题吗? - stackoverflow.com/q/19213535/2125723
标签: python regex python-3.x