Python - 如何将 re.finditer 与多个模式一起使用

Python - How to use re.finditer with multiple patterns

我想在一个字符串中搜索 3 个单词并将它们放入一个列表中 类似于:

sentence = "Tom once got a bike which he had left outside in the rain so it got rusty"

pattern = ['had', 'which', 'got' ]

答案应该是这样的: ['got', 'which','had','got'] 我还没有找到以这种方式使用 re.finditer 的方法。遗憾的是我需要使用 finditer 而不是 findall

您可以从搜索词列表构建模式,然后使用 finditer:

返回的匹配项的列表理解构建输出列表
import re

sentence = "Tom once got a bike which he had left outside in the rain so it got rusty"

pattern = ['had', 'which', 'got' ]
regex = re.compile(r'\b(' + '|'.join(pattern) + r')\b')
# the regex will be r'\b(had|which|got)\b'

out = [m.group() for m in regex.finditer(sentence)]
print(out)

# ['got', 'which', 'had', 'got']

想法是将 pattern 列表的条目组合成一个正则表达式 ors。 然后,您可以使用以下代码片段:

import re

sentence = 'Tom once got a bike which he had left outside in the rain so it got rusty. ' \
           'Luckily, Margot and Chad saved money for him to buy a new one.'

pattern = ['had', 'which', 'got']

regex = re.compile(r'\b({})\b'.format('|'.join(pattern)))
# regex = re.compile(r'\b(had|which|got)\b')

results = [match.group(1) for match in regex.finditer(sentence)]

print(results)

结果是['got', 'which', 'had', 'got'].