1import re
2s = "Example String"
3replaced = re.sub('[ES]', 'a', s)
4print replaced
5# will print 'axample atring'
1import re
2line = re.sub(r"</?\[\d+>", "", line)
3
4# Comented version
5line = re.sub(r"""
6 (?x) # Use free-spacing mode.
7 < # Match a literal '<'
8 /? # Optionally match a '/'
9 \[ # Match a literal '['
10 \d+ # Match one or more digits
11 > # Match a literal '>'
12 """, "", line)
13
1import re
2s = "Example String"
3replaced = re.sub('[ES]', 'a', s)
4print replaced
1import re
2
3s = 'aaa@xxx.com bbb@yyy.com ccc@zzz.com'
4
5print(re.sub('[a-z]*@', 'ABC@', s))
6# ABC@xxx.com ABC@yyy.com ABC@zzz.com
7
1# Limit the maximum number of pattern occurrences to be replaced
2# replace three occurrence of space with '-'
3target_str = "a b c d e f"
4res_str = re.sub(r"\s", "-", target_str, count=3)
5print(res_str)
6# Output a-b-c-d e f