我有一个单词列表(find_list),想在文本中找到这些单词,并有一个包含这些单词的表达式列表(scape_list),当它们出现在文本中时,想要跳过这些表达式。
使用以下代码,我可以在文本中找到所有的单词:
find_list = ['name', 'small']
scape_list = ['small software', 'company name']
text = "My name is Klaus and my middle name is Smith. I work for a small company. The company name is Small Software. Small Software sells Software Name."
final_list = []
for word in find_list:
s = r'\W{}\W'.format(word)
matches = re.finditer(s, text, (re.MULTILINE | re.IGNORECASE))
for word_ in matches:
final_list.append(word_.group(0))
最终列表如下:
[' name ', ' name ', ' Name.', ' small ']
final_list和scape_list总是在更新。因此,我认为正则表达式是一个不错的方法。