我希望它遍历列表并检查列表中的每个项目是否在 var txt 中,如果它们存在,则用空格替换它们。如您所见,我只能将列表中的第一项替换掉。我如何让它遍历列表中的每个项目?谢谢。
import re
txt='affirmed and the affirmance and AFFIRMED and Affirm case'
wordList = ['case', 'affirm\w+', '(ca\w+)']
for word in wordList:
out = re.sub(wordList[0], '', txt, re.I)
#out = re.sub(r'\Abaffirm.+', '', txt, re.IGNORECASE)
print txt
print out
输出:
affirmed and the affirmance and AFFIRMED and Affirm case
affirmed and the affirmance and AFFIRMED and Affirm