一、局部匹配(只匹配一个结果)
1.re.match()函数 :从源字符的串的开头进行匹配
2.re.search()函数 :在全文中进行检索并匹配
从例子中看区别:
import re
#string ="apythonhellomypythonisouorpythonend"
string = "hellomypythonispythonourpythonend"
pattern=".python."
result =re.match(pattern,string)
result2 = re.search(pattern,string)
print (result)
print (result2)
运行结果:
二、全局匹配(找出所有的匹配结果)
1)使用re.compile()对正则式进行预编译
2)编译后,使用findall()根据正则表达式从源字符串中奖匹配的结果全部找出
import re
#string ="apythonhellomypythonisouorpythonend"
string = "hellomypythonispythonourpythonend"
pattern=".python."
su = re.compile(patte