Rick
Rick

Reputation: 147

Match max 5 words before and after a certain word

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

Answers (2)

pmntang
pmntang

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

Amber
Amber

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

Related Questions