[Solved] Derive words from string based on key words


You can solve it using regex. Like this e.g.

import re


expected_output = re.findall('(?:{0})\s+?([^\s]+)'.format('|'.join(key_words)), text_string)

Explanation

  1. (?:{0}) Is getting your key_words list and creating a non-capturing group with all the words inside this list.
  2. \s+? Add a lazy quantifier so it will get all spaces after any of the former occurrences up to the next character which isn’t a space
  3. ([^\s]+) Will capture the text right after your key_words until a next space is found

Note: in case you’re running this too many times, inside a loop i.e, you ought to use re.compile on the regex string before in order to improve performance.

2

solved Derive words from string based on key words