title: preg_match while the keyword attached to something else
body: <p>Suppose that I have a long string, and I wanna cut it in a way to contain a keyword (the keyword is based on a pattern, like {query:ABCD:1234}), 5 words before the keyword and 5 words after it. If the keyword appears alone, I can find it using this regex:</p>
  
  <pre><code>preg_match("/(([^ ]+ ){5})$keyword (([^ ]+ ){5})/", $text, $matches);
  </code></pre>
  
  <p>But if the keyword attached to some other characters (e.g. a comma or dot), the above regex cannot find it. If I change the regex to this one:</p>
  
  <pre><code>preg_match("/(([^ ]+ ){5})$keyword(([^ ]+ ){5})/", $text, $matches);
  </code></pre>
  
  <p>It finds the keyword attached to the comma, but not alone.</p>
  
  <p>How can I cover both cases, keyword alone and with attachment, with a regex?</p>