我认为您不需要正则表达式来检查字符串中是否存在。相反,使用
in
和
re
拆分字符串:
import re
creategarbageterms = {'tim_tam' : ['tim_tam','yummy_tim_tam', 'berry_tim_tam'],
'pudding': ['pudding', 'chocolate_pudding', 'biscuits', 'tiramusu'],
'ice_cream': ['ice_cream', 'vanilla_ice_cream']}
s = ['wow_yummy_tim_tam', 'melted_tim_tam, berry_tim_tam', 'cherry_berry_tim_tam', 'wow_tam', 'wow_m', 'wow_ti', 'Wow_tim_t']
for c in s:
truthy = any(any(i in c for i in b) for a, b in creategarbageterms.items())
if truthy:
print("Yes")
else:
print("no")
输出:
Yes
Yes
Yes
no
no
no
no