0

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

2
  • Then why you tagged findall? Commented Dec 8, 2019 at 16:50
  • What have you tried? Can you show your attempt? Commented Dec 8, 2019 at 16:54

2 Answers 2

4

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']
Sign up to request clarification or add additional context in comments.

Comments

-1

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'].

2 Comments

Your regex would also match the 'got' in 'Margot' and 'had' in 'Chad'
Thanks, I incorporated your hint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.