Searching and Replacing with sub() [and subn()] : sub « Regular Expressions « Python Tutorial






import re

re.sub('X', 'Mr. Smith', 'attn: X\n\nDear X,\n')
re.subn('X', 'Mr. Smith', 'attn: X\n\nDear X,\n')
print re.sub('X', 'Mr. Smith', 'attn: X\n\nDear X,\n')
re.sub('[ae]', 'X', 'abcdef')
re.subn('[ae]', 'X', 'abcdef')

# Splitting (on Delimiting Pattern) with split()
re.split(':', 'str1:str2:str3')








16.7.sub
16.7.1.re.sub substitutes the leftmost, nonoverlapping occurrences of a pattern with a given replacement
16.7.2.Matching at the End of a String
16.7.3.Matching Whole Words
16.7.4.Searching and Replacing with sub() [and subn()]