title: Java regex to remove duplicate substrings from string
body: <p>I'm trying to build a regex to "reduce" duplicate consecutive substrings from a string in Java. For example, for the following input:</p>
  
  <pre><code>The big black dog big black dog is a friendly friendly dog who lives nearby nearby.
  </code></pre>
  
  <p>I'd like to get the following output:</p>
  
  <pre><code>The big black dog is a friendly dog who lives nearby.
  </code></pre>
  
  <p>This is the code I have so far:</p>
  
  <pre><code>String input = "The big black dog big black dog is a friendly friendly dog who lives nearby nearby.";
  
  Pattern dupPattern = Pattern.compile("((\\b\\w+\\b\\s)+)\\1+", Pattern.CASE_INSENSITIVE);
  Matcher matcher = dupPattern.matcher(input);
  
  while (matcher.find()) {
      input = input.replace(matcher.group(), matcher.group(1));
  }
  </code></pre>
  
  <p>Which is working out fine for all duplicate substrings except for the end of the sentence:</p>
  
  <pre><code>The big black dog is a friendly dog who lives nearby nearby.
  </code></pre>
  
  <p>I understand that my regex requires a whitespace after each word in the substring, meaning it won't catch cases with a period instead of a space. I can't seem to find a workaround for this, I have tried playing around with the capture groups and also changing the regex to look for a whitespace or a period instead of just a whitespace, but this solution will only work if there is a period after each duplicate part of the substring ("nearby.nearby.").</p>
  
  <p>Can somebody point me in the right direction? Ideally the inputs for this method will be short paragraphs and not just one-liners. </p>