title: REGEX - Matching any character which repeats n times
body: <p>How to match any character which repeats <code>n</code> times?</p>
  
  <p>Example:</p>
  
  <pre><code>for input: abcdbcdcdd
  for n=1:   ..........
  for n=2:    .........
  for n=3:     .. .....
  for n=4:      .  . ..
  for n=5:   no matches
  </code></pre>
  
  <p>After several hours my best is this expression</p>
  
  <pre><code>(\w)(?=(?:.*\1){n-1,}) //where n is variable
  </code></pre>
  
  <p>which uses lookahead. However the problem with this expression is this:</p>
  
  <pre><code>for input: abcdbcdcdd
  for n=1    .......... 
  for n=2     ... .. .
  for n=3      ..  .
  for n=4       .
  for n=5    no matches
  </code></pre>
  
  <p>As you can see, when lookahead matches for a character, let's look <em><code>for n=4</code></em> line, <code>d</code>'s lookahead assertion satisfied and first <code>d</code> matched by regex. But remaining <code>d</code>'s are not matched because they don't have 3 more <code>d</code>'s ahead of them.</p>
  
  <p>I hope I stated the problem clearly. Hoping for your solutions, thanks in advance.</p>