code: def scanf_compile(format, collapseWhitespace=True):
      
  
      format_pat = ""
      cast_list = []
      i = 0
      length = len(format)
      while i < length:
          found = None
          for token, pattern, cast in scanf_translate:
              found = token.match(format, i)
              if found:
                  if cast: # cast != None
                      cast_list.append(cast)
                  groups = found.groupdict() or found.groups()
                  if groups:
                      pattern = pattern % groups
                  format_pat += pattern
                  i = found.end()
                  break
          if not found:
              char = format[i]
              # escape special characters
              if char in "|^$()[]-.+*?{}<>\\":
                  format_pat += "\\"
              format_pat += char
              i += 1
      if DEBUG:
          print("DEBUG: %r -> %s" % (format, format_pat))
      if collapseWhitespace:
          format_pat = re.sub(r'\s+', r'\\s+', format_pat)
  
      format_re = re.compile(format_pat)
      return format_re, cast_list
docs: Translate the format into a regular expression
  
      For example:
      >>> format_re, casts = scanf_compile('%s - %d errors, %d warnings')
      >>> print format_re.pattern
      (\S+) \- ([+-]?\d+) errors, ([+-]?\d+) warnings
  
      Translated formats are cached for faster reuse