title: java regular expression to remove duplicated words
body: <p>I am working on a problem that removes duplicated words from a string. E.g., </p>
  
  <p>Input: <code>Goodbye bye bye world world world</code></p>
  
  <p>Output: <code>Goodbye bye world</code></p>
  
  <p>I have got a working pattern from online resources, but I am not able to understand all the content in it. </p>
  
  <pre><code>    String pattern = "\\b(\\w+)(\\b\\W+\\b\\1\\b)*";
  </code></pre>
  
  <p>Here is my understanding:</p>
  
  <ol>
  <li>the initial <code>\\b</code> is to match word bounaries</li>
  <li><code>(\\w+)</code> matches one or more characters</li>
  <li><p>in this expression : <code>(\\b\\W+\\b\\1\\b)*</code></p>
  
  <p>a. <code>\\b</code> matches word boundaries</p>
  
  <p>b. <code>\\W+</code> matches one or more non-word characters</p>
  
  <p>c. <code>\\b</code> again matches a word bounary</p>
  
  <p>d. <code>\\1</code> ??? I dont know what this is for, but it wont work without this</p>
  
  <p>c. <code>\\b</code> again matches for a word bounary</p></li>
  </ol>
  
  <p>As you can see, my main confusion is about item 3 and especially <code>\\1</code>.
  Anyone can explain it more clearly?</p>