title: Match a whole word in a string using dynamic regex
body: <p>I am looking to see whether a word occurs in a sentence using regex. Words are separated by spaces, but may have punctuation on either side. If the word is in the middle of the string, the following match works (it prevents part-words from matching, allows punctuation on either side of the word).</p>
  
  <pre><code>match_middle_words = " [^a-zA-Z\d ]{0,}" + word + "[^a-zA-Z\d ]{0,} "
  </code></pre>
  
  <p>This won't however match the first or last word, since there is no trailing/leading space. So, for these cases, I have also been using:</p>
  
  <pre><code>match_starting_word = "^[^a-zA-Z\d]{0,}" + word + "[^a-zA-Z\d ]{0,} "
  match_end_word = " [^a-zA-Z\d ]{0,}" + word + "[^a-zA-Z\d]{0,}$"
  </code></pre>
  
  <p>and then combining with</p>
  
  <pre><code> match_string = match_middle_words  + "|" + match_starting_word  +"|" + match_end_word 
  </code></pre>
  
  <p>Is there a simple way to avoid the need of three match terms. Specifically, is there a way of specifying 'ether a space or the start of file (i.e. "^") and similar, 'either a space or the end of the file (i.e. "$")?</p>