1、打开JUPYTER NOTEBOOK,新建一个空白的PY文档。
2、import re首先要引入re模块,才能进行下去。
3、a = re.compile(r'^my')a.search('my name is Peter')如果我们以^运算符打头,那么就是要以某段字符为开始来寻找是否有。
4、a.search('My name is Peter') == None要注意的是要区别大写和小写,不然会判断不了。
5、b = re.compile(r'^12')b.search('123 is the number.')字符串的数字也是可以进行判断的。
6、c = re.compile(r'\d$')c.search('the correct number is 8329')$运算符就是表示以某个字符为结尾进行搜索。
7、c = re.compile(r'\d$')c.search('the correct number is 8329.') == None我们经常回出错的就是要注意句子后有没有句号。
8、d = re.compile(r'^hello, my friend.$')d.search('hello, my friend.')我们还可以同时使用^$来定义开始和结尾。
9、d = re.compile(r'^my$')d.search('hello, my friend.') == None但是用这个运算符就要表示整段字符串了。