import re
result = re.sub(pattern, repl, string, count=0, flags=0);
result = re.sub('abc', '', input) # Delete pattern abc
result = re.sub('abc', 'def', input) # Replace pattern abc -> def
result = re.sub(r's+', ' ', input) # Eliminate duplicate whitespaces using wildcards
result = re.sub('abc(def)ghi', r'1', input) # Replace a string with a part of itself
# From Grepper Docs
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
'pro--gram files'
>>> re.sub(r'sANDs', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
'Baked Beans & Spam'
new_string = re.sub(r"xxx|yyy", "abc", a_string)
re.sub(pattern, replace, string)
re.sub(pattern,replacement,string)
re.sub finds all matches of pattern in string and replaces them
with replacement.
#Example
re.sub("[^0-9]","","abcd1234") #Returns 1234
result = re.sub("(d+) (w+)", r"2 1")
result = re.sub("(?<number>d+) (?<word>w+)", r"g<word> g<number>")