包含瓦尔、卡夫,但不包含到、额
pd.Series('的身份拉尔卡夫卡瓦尔').str.contains('(?=.*瓦尔)(?=.*卡夫)^(?!.*到)^(?!.*额)')
文字处理应用:
kw = "track or 尼龙&网布&鞋not女&卫衣"
# [ ''.join(['&'.join(k[0])] if len(k)==1 else [''.join(['(?=.*'+w+')' for w in k[0]]), ''.join('^(?!.*' + y + ')' for y in k[1])]) for k in [[j.split('&') for j in i.split('not')] for i in kw.replace(' ', '').split('or')]]
[''.join([''.join(['(?=.*'+j+')' for j in i.split('not')[0].split('&')]), ''.join(['^(?!.*'+j+')' for j in i.split('not')[1].split('&')])]) if 'not' in i else ''.join(['(?=.*'+j+')' for j in i.split('&')]) for i in kw.replace(' ', '').split('or')]
>>> ['track', '(?=.*尼龙)(?=.*网布)(?=.*鞋)^(?!.*女)^(?!.*卫衣)']