Reputation: 147
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
Upvotes: 4
Views: 3120
Reputation: 21
Here is what seems to work regarding the question asked.
(?:\s*\w+\s+){0,5}the(?:\s*\w+\s*){0,5}
Upvotes: 1
Reputation: 527213
(?:[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.
Upvotes: 8