'Replaces a regular expression pattern with repl in each "name: value"
header line.
Returns:
The number of replacements made.'

def replace(self, pattern, repl, flags=0, count=0):

    if isinstance(pattern, str):
        pattern = strutils.escaped_str_to_bytes(pattern)
    if isinstance(repl, str):
        repl = strutils.escaped_str_to_bytes(repl)
    pattern = re.compile(pattern, flags)
    replacements = 0
    flag_count = (count > 0)
    fields = []
    for (name, value) in self.fields:
        (line, n) = pattern.subn(repl, ((name + ':    ') + value), count=count)
        try:
            (name, value) = line.split(':    ', 1)
        except ValueError:
            pass
        else:
            replacements += n
            if flag_count:
                count -= n
                if (count == 0):
                    break
        fields.append((name, value))
    self.fields = tuple(fields)
    return replacements
