title: RegExp regular expression find & replace whole words only
body: <p>I should preface this by stating that I'm working with VB6 &amp; RegExp</p>
  
  <p>I'm attempting to find and substitute whole words, by "whole words" I mean a valid match is not a substring of another word, although some special characters will be ok. I'm a novice at regular expressions. This is what I was trying:</p>
  
  <pre><code>([^a-z]+)(Foo)([^a-z]+)
  </code></pre>
  
  <p>It seems close but I'm having some trouble in certain situations.</p>
  
  <p>For example, if I find the string</p>
  
  <pre><code>Foo Foo
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Foo(Foo)
  </code></pre>
  
  <p>or anywhere a line ends with Foo and the following line begins with Foo</p>
  
  <pre><code>This is a line with Foo
  Foo starts the next line
  </code></pre>
  
  <p>In any of these cases only the first Foo is matched.</p>
  
  <p>Well, maybe it isn't a problem with the match but rather my replace method. I don't know exactly how I can verify that. I'm using groups to replace whatever bounding char is matched by the expression, like so:</p>
  
  <pre><code>regEX.Replace(source, "$1" &amp; newstring &amp; "$3")
  </code></pre>
  
  <p>So in summary I want to avoid matching:
  FooBar
  BarFoo</p>
  
  <p>Any of the following would be valid matches:</p>
  
  <p>Foo Foo<br/>
  Foo Bar<br/>
  Foo_bar<br/>
  Foo.bar<br/>
  Foo, bar<br/>
  Foo(bar)<br/>
  Foo(Foo)<br/></p>
  
  <p>If anyone can kindly show me the proper way to do this I would much appreciate it!</p>
  
  <p><strong>edited</strong></p>
  
  <p>Looks like I spoke a little too soon regarding the first solution below. After a little testing and some further reading, I see that underscore is a word char and thus the above pattern won't match it. I came up with this which does the trick, is there a better way?</p>
  
  <pre><code>(\b)(Foo)(\b|_)
  
  regEX.Replace(source, "$1" &amp; newstring &amp; "$3")
  </code></pre>
  
  <p>It works, but seems a little sloppy.</p>