gpt4 book ai didi

python - 用于python中单词排除的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:20 46 4
gpt4 key购买 nike

我有一个正则表达式 '[\w_-]+' 允许字母数字字符或下划线。

我不想让 python 列表中有一组单词

listIgnore = ['summary', 'config']

需要对正则表达式进行哪些更改?

P.S: 我是正则表达式的新手

最佳答案

>>> line="This is a line containing a summary of config changes"
>>> listIgnore = ['summary', 'config']
>>> patterns = "|".join(listIgnore)
>>> print re.findall(r'\b(?!(?:' + patterns + r'))[\w_-]+', line)
['This', 'is', 'a', 'line', 'containing', 'a', 'of', 'changes']

关于python - 用于python中单词排除的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19829015/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com