title: Regex to match the longest repeating substring
body: <p>I'm writing regular expression for checking if there is a substring, that contains at least 2 repeats of some pattern next to each other. I'm matching the result of regex with former string - if equal, there is such pattern. Better said by example: 1010 contains pattern 10 and it is there 2 times in continuous series. On other hand 10210 wouldn't have such pattern, because those 10 are not adjacent. </p>
  
  <p>What's more, I need to find the longest pattern possible, and it's length is at least 1. I have written the expression to check for it <code>^.*?(.+)(\1).*?$</code>. To find longest pattern, I've used non-greedy version to match something before patter, then pattern is matched to group 1 and once again same thing that has been matched for group1 is matched. Then the rest of string is matched, producing equal string. But there's a problem that regex is eager to return after finding first pattern, and don't really take into account that I intend to make those substrings before and after shortest possible (leaving the rest longest possible). So from string <code>01011010</code> I get correctly that there's match, but the pattern stored in group 1 is just <code>01</code> though I'd except <code>101</code>. </p>
  
  <p>As I believe I can't make pattern "more greedy" or trash before and after even "more non-greedy" I can only come whit an idea to make regex less eager, but I'm not sure if this is possible. </p>
  
  <p>Further examples:</p>
  
  <pre><code>56712453289 - no pattern - no match with former string
  22010110100 - pattern 101 - match with former string (regex resulted in 22010110100 with 101 in group 1)
  5555555 - pattern 555 - match
  1919191919 - pattern 1919 - match
  191919191919 - pattern 191919 - match
  2323191919191919 - pattern 191919 - match
  </code></pre>
  
  <p>What I would get using current expression (same strings used):</p>
  
  <pre><code>no pattern - no match
  pattern 2 - match
  pattern 555 - match
  pattern 1919 - match
  pattern 191919 - match
  pattern 23 - match
  </code></pre>