Match no more than 5 words before and after a specific word

I need a regular expression that will contain no more than 5 words before and after a certain word. Example:

a certain word is "

line - "John Ely begins to play for the Dodgers, and old friend Takashi Saito starts the Brewers in what will essentially be a bullpen game for Milwaukee."

There should be 2 results:

John Ely begins the start for Dodgers, and Takashi's old friend

Takashi Saito begins to drink brewers in what will be essentially

Any ideas ???

thank

+3
source share
1 answer
(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}the(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}

Note that most regex engines will not handle matching matches.

+5
source

All Articles