I need a regex that will match at most 5 words before and after a certain word. Example:
the certain word is "the"
the string is "John Ely gets start for the Dodgers, while old friend Takashi Saito gets start for the Brewers in what will essentially be a bullpen game for Milwaukee."
There should be 2 results:
John Ely gets start for the Dodgers, while old friend Takashi
Takashi Saito gets start for the Brewers in what will essentially
Any ideas???
Thanks
(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}the(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}
Note however that most regex engines won't deal with overlapping matches.
Here is what seems to work regarding the question asked.
(?:\s*\w+\s+){0,5}the(?:\s*\w+\s*){0,5}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With