title: Python Regex: set combinations of prefix length
body: <p>I am trying to find all the combinations of a set of characters in a string, BUT the set of characters found have to have a specific length. I had thought of using the set function [] with the number of occurrences function {:}, I can't seem to get them to work together. </p>
  
  <p>The example should return 'this' and 'hits'</p>
  
  <p>Thank you. </p>
  
  <pre><code>def test_patterns(text, patterns=[]):
      """Given source text and a list of patterns, look for
      matches for each pattern within the text and print
      them to stdout.
      """
      # Look for each pattern in the text and print the results
      for pattern, desc in patterns:
          print "Pattern %r (%s)\n" % (pattern, desc)
          print ' %r' % text
          for match in re.finditer(pattern, text):
              s = match.start()
              e = match.end()
              #substr = match.group()
              substr = text[s:e]
              n_backslashes = text[:s].count('\\')
              prefix = '.' * (s + n_backslashes)
              print ' %s%r' % (prefix, substr)
          print
      return
  if __name__ == '__main__':
      test_patterns('Does this text contain hits or matches?', [('[this]{4:4}+', "Description"),])
  raw_input()
  </code></pre>