1、打开JUPYTER NOTEBOOK,新建一个空白的PY文档。

2、引入re模块。
import re执行指令。

3、a = re.compile(r'Peter')
a.sub('Alice', 'My name is Peter.')
sub可以替换某个字符串。

4、b = re.compile(r'888')
b.sub('666', 'the number is 888.')
除了字符串以外,字符串里的数字也是可以进行替换的。

5、c = re.compile(r'ui')
c.sub('ae', 'uiUIuiUI')
c = re.compile(r'ui', re.I)
c.sub('ae', 'uiUIuiUI')
替换的时脂秤候要注意大小写,用re.I可以辅助一下。

6、d = re.compile(r'o')
d.sub('$', 'i am an intership.')
如果没有找到要替换的,那么就会返回原来的句子。

7、e = re.compile(r'\d')
e.sub('$', '123 dlsjlfj 3828 jfsjdf r23rj2jr')
通用的数字\d这样来表示会节省很多功夫,也是可以替换的。

8、f = re.compile(r'NAME (\w)\w*')
f.sub(r'\1***', 'NAME Peter is the father of NAME Alice, but NAME Ben is not the monther of NAME Chris.'国宋)
除了普通各此霸的替换以外,还可以替换完以后只显示其中的开头的第一个字母。
