Note for those who still deal with Python 2.6.x installations or older. Python documentation for 2.6 re says:
re.sub(pattern, repl, string[, count])
re.compile(pattern[, flags])
This means you cannot pass flags directly to sub. They can only be used with compile:
regex = re.compile('class', re.I)
regex.sub("function", "Class object")