import re
result = re.sub(pattern, repl, string, count=0, flags=0);
# 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'
import re
# Regular Expression pattern 'ub' matches the string at "Subject" and "Uber". As the CASE has been ignored, using Flag, 'ub' should match twice with the string Upon matching, 'ub' is replaced by '~*' in "Subject", and in "Uber", 'Ub' is replaced.
print(re.sub('ub', '~*', 'Subject has Uber booked already',
flags=re.IGNORECASE))
# Consider the Case Sensitivity, 'Ub' in "Uber", will not be replaced.
print(re.sub('ub', '~*', 'Subject has Uber booked already'))
# As count has been given value 1, the maximum times replacement occurs is 1
print(re.sub('ub', '~*', 'Subject has Uber booked already',
count=1, flags=re.IGNORECASE))
# 'r' before the pattern denotes RE, s is for start and end of a String.
print(re.sub(r'sANDs', ' & ', 'Baked Beans And Spam',
flags=re.IGNORECASE))
re.subn(pattern, repl, string, count=0, flags=0)
import re
print(re.subn('ub', '~*', 'Subject has Uber booked already'))
t = re.subn('ub', '~*', 'Subject has Uber booked already',
flags=re.IGNORECASE)
print(t)
print(len(t))
# This will give same output as sub() would have
print(t[0])
re.sub(pattern, repl, string, count=0, flags=0)