If you can slurp whole input:
perl -e'undef$/;
$a=<>;
print "$_\n" for $a=~m/(?:\b\S+\b\s+){5}your regexp(?:\s+\b\S+\b){5}/sg
' <input
— 5 words of context.
solved how to print n words of context while searching text [closed]