re正则表达式公式讲解5

1.refullmatch() 完全匹配字符串则返回object,否则返回None

import re

s = "max@123uyt146"

print(re.fullmatch("w+@w+",s))

# <_sre.SRE_Match object; span=(0, 13), match='max@123uyt146'>

2.re.compile()  

两种方法返回的同一个对象,有什么区别?

compile()时先编译好,再匹配,如果需要匹配的字符串多的话能省很多时间。

fullmatch(patterns,str)  一边编译一遍匹配,如果只有一个字符串,compile和fullmatch没区别,如果字符串多的话,compile比fullmatch好。

import re

s = "max@123uyt146"

pattern = re.compile("w+@w+")

print(pattern.fullmatch(s))

# <_sre.SRE_Match object; span=(0, 13), match='max@123uyt146'>

print(re.fullmatch("w+@w+",s))

# <_sre.SRE_Match object; span=(0, 13), match='max@123uyt146'>

  

  

原文地址:https://www.cnblogs.com/Roc-Atlantis/p/8918629.html