Python method to replace a pattern with another pattern, found in a string
import re
def replace(chars, subpattern, replacement, max_replacements=0):
pattern = re.compile(subpattern, re.M)
return pattern.sub(replacement, chars, max_replacements if max_replacements > 0 else 0)