$s = 'I have somwhat large articles of text (returned from a search), what I\'d like to do is capture the word and X number of words before and after it while tagging the matching word in the captured text.';; print $s =~ m[ ( (?: \S+ \s+ ){3} X (?: \s+ \S+ ){3} ) ]x;; the word and X number of words