title: grep- why must there be word boundaries around back references?
body: <p>I'm just curious why grep matches things in this way.</p>
  
  <p>For example, let's say I'm trying to find a word that occurs twice in a sentence (and not as parts of other words). So I'm trying to find lines like the following :</p>
  
  <pre><code>hello everybody hello
  </code></pre>
  
  <p>and not like the following :</p>
  
  <pre><code>hello everybody hellopeople 
  </code></pre>
  
  <p>Then why does the following grep expression work :</p>
  
  <pre><code>grep -E '(\&lt;.*\&gt;).*\&lt;\1\&gt;' file
  </code></pre>
  
  <p>and not the following :</p>
  
  <pre><code>grep -E '(\&lt;.*\&gt;).*\1' file
  </code></pre>
  
  <p>I would have thought that the second one would work because the word boundaries (\&lt; and \>) are inside the parentheses for the second match, but it doesn't. It just seems rather confusing that one has to put word boundaries around the back reference, can someone explain why grep matches lines in this way, or maybe elaborate on this idea further?</p>