re的matche与search方法
matche的函数构成:
matche(string[,pos[,endpos]])
string 为目标文本
pos 开始索引或者string开始的下标。
endpos 结束搜索的索引。
使用mache时开始标志默认从开头开始,如果开始无法匹配会自动返回none:
如图ss不是字符串的首字母所以报错:
import re str1 = 'dssddashello wwwhahah' reg = re.compile(r'(ss.*www)(h)') result=reg.match(str1) print (result.groups(str1))
C:\Python27\python.exe E:/untitled/reget/Re.py Traceback (most recent call last): File "E:/untitled/reget/Re.py", line 10, in <module> print (result.groups(str1)) AttributeError: 'NoneType' object has no attribute 'groups' Process finished with exit code 1
修改后:
import re str1 = 'dssddashello wwwhahah' reg = re.compile(r'(dss.*www)(h)') result=reg.match(str1) print (result.groups(str1))
C:\Python27\python.exe E:/untitled/reget/Re.py ('dssddashello www', 'h') Process finished with exit code 0
使用search时当开头无法匹配时,下标自动加一继续匹配知道找到目标:
str2 = 'cc'+str1 result1=reg.search(str2) print (result1.groups())
由于使用字符串一的方法所以输出结果与前者一致:
C:\Python27\python.exe E:/untitled/reget/Re.py ('dssddashello www', 'h') ('dssddashello www', 'h') Process finished with exit code 0
记住使用search会造成重复计算:
reg1=re.compile(r'\w*(dd.*)(www.*.a)') result3= reg1.match(str1) print (result3.groups())
('ddashello ', 'wwwhaha') Process finished with exit code 0
使用match+\w与search的效果一直并且不用重复计算
re的spilt与findall用法
split 使用分隔符将字符串分解后返回:
str3='a122b2344v321n4m5b6' reg2=re.compile(r'\d+') result4=reg2.split(str3) print (result4)
['a', 'b', 'v', 'n', 'm', 'b', ''] Process finished with exit code 0
规则是以\d(数字)+(表示忽略数字个数)为分隔条件。
findall查找出全部符合条件的字符串以列表形式返回:
result5=reg2.findall(str3) print (result5)
['122', '2344', '321', '4', '5', '6'] Process finished with exit code 0
finditer找出所有结果的可迭代对象:
result6=reg2.finditer(str3) for i in result6: print (i.group())
122
2344
321
4
5
6
Process finished with exit code 0
浙公网安备 33010602011771号