title: R grep whole words separated by special characters
body: <p>Suppose there is a vector of sequences of the form <code>"foo"</code> or <code>"foo|baz|bar"</code> (one single word or multiple words separated by special character like <code>"|"</code>), and we are also given a word and we want to find to which items of the vector it has a <strong>whole word</strong> match. </p>
  
  <p>For example the word <code>"foo"</code> has a whole match in <code>"foo|baz|bar"</code>, but not a whole match in either <code>"foobaz|bar"</code> or <code>"bazfoo"</code>. </p>
  
  <p>First I tried to use <code>"\\b"</code> that indicates either the start or the end edges of a whole word and it works successfully:</p>
  
  <pre><code>grep("\\bfoo\\b", "foo")         # match
  grep("\\bfoo\\b", "foobaz|bar")  # mismatch    
  grep("\\bfoo\\b", "bazfoo")      # mismatch
  </code></pre>
  
  <p>Then I tried to add <code>"|"</code> as the other possible separator of both ends, and group it with <code>"\\b"</code> using <code>[</code> and <code>]</code>:</p>
  
  <pre><code>grep("[|\\b]foo[|\\b]", "foo|baz|bar")  # mismatch!
  grep("[|\\b]foo[|\\b]", "foo")          # mismatch!
  </code></pre>
  
  <p>Later I found <code>\\b</code> is not indicator of <em>start</em> or <em>end</em> of the character string, but start or end of a whole word (so many characters like space and <code>,|-^.</code> but <strong>not</strong> numbers and underline <code>_</code> separate whole words). So <code>"[|\\b]foo[|\\b]"</code> matches to all of these strings: <code>"foo", "foo|bar|baz", "foo-bar", "baz foo|bar"</code> but not to <code>"foo_bar"</code> or <code>"foo2"</code>.</p>
  
  <p>But my question still remains: Why <code>"[|\\b]foo[|\\b]"</code> pattern fails to match with <code>"foo"</code>?</p>