subst = '\x1b[1;41m' + '\\g<0>' + '\x1b[0m'
file_contents = re.sub(regex, subst, file_contents)
print(file_contents)
the_file.close()
Finding and substituting a list of words in a file using regex in Python