smac89
7/27/2016 - 4:56 AM

Python method to replace a pattern with another pattern, found in a string

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)