I have a string. I want to cut the string up into substrings that include a number-containing word surrounded by (up to) 4 words on either side. If the substrings overlap they should combine.
Sampletext = "by the way I know 54 how to take praise for 65 excellent questions 34 thank you for asking appreciated."
re.findall('(\s[*\s]){1,4}\d(\s[*\s]){1,4}', Sampletext)
desired output = ['the way I know 54 how to take praise', 'to take praise for 65 excellent questions 34 thank you for asking']
Overlapping Matches: Use Lookaheads
This will do it:
What is a Word?
The output depends on your definition of a word. Here, in a word, I have allowed digits. This produces the following output.
Output (allowing digits in words)
Option 2: No digits in Words
Output 2
Option 3: extending to four uninterrupted non-digit words
Based on your comments, this option will extend to the left and right of the pivot until four uninterrupted non-digit words are matched. Commas are ignored.
Output 3