DownKick
DownKick

Reputation: 13

Python - How to use re.finditer with multiple patterns

I want to search 3 Words in a String and put them in a List something like:

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

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

and the answer should look like: ['got', 'which','had','got'] I haven't found a way to use re.finditer in such a way. Sadly im required to use finditer rather that findall

Upvotes: 0

Views: 6219

Answers (2)

fyaa
fyaa

Reputation: 676

The idea is to combine the entries of the pattern list to form a regular expression with ors. Then, you can use the following code fragment:

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)

The result is ['got', 'which', 'had', 'got'].

Upvotes: -1

Thierry Lathuille
Thierry Lathuille

Reputation: 24231

You can build the pattern from your list of searched words, then build your output list with a list comprehension from the matches returned by 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']

Upvotes: 4

Related Questions