正則的matche方法和search方法
match(string[, pos[, endpos]])
string:匹配使用的文字,
pos: 文字中正則表示式開始搜尋的索引。及開始搜尋string的下標
endpos: 文字中正則表示式結束搜尋的索引。
如果不指定pos,預設是從開頭開始匹配,如果匹配不到,直接返回None
import re reg = re.compile(r'\w*(hello w.*)(hello v.*)') #print(dir(reg)) a = 'aahello world hello vfx' result = reg.match(a) print(result) print如果不加 \w* 返回是 None。如果不指定pos的話,預設是從字串開始位置匹配,匹配不到就返回None(result.groups())
2.search的方法
search(string[, pos[, endpos]])
這個方法用於查詢字串中可以匹配成功的子串。從string的pos下標處起嘗試匹配reg,如果reg結束時仍可匹配,則返回一個Match物件;若無法匹配,則將pos加1後重新嘗試匹配;直到pos=endpos時仍無法匹配則返回None。
import re reg = re.compile(r'\w*(hello w.*)(hello v.*)') #print(dir(reg)) a = 'aahello world hello vfx' result = reg.match(a) print(result) print(result.groups()) b = 'aa' +a result2 = reg.match(b) print(result2) result3 = reg.search(b) print(result3) print(result3.groups())
推薦使用matche,比較精準,快速。search要全部都搜,很慢。
3.re的split的使用方法
split(string[, maxsplit])
按照能夠匹配的子串將string分割後返回列表。maxsplit用於指定最大分割次數,不指定將全部分割。
import re p = re.compile(r'\d+') print(p.split('one1two2three3four4'))
返回結果為:
['one', 'two', 'three', 'four', '']
4.正則物件的findall方法
findall(string[, pos[, endpos]])
搜尋
import re
p = re.compile(r'\d+')
print(findall('one1two2three3four4'))
結果:
['1', '2', '3', '4']
結果:findall是把匹配到的字串最後一列表的形式返回回去
5.正則物件的finditer方法
finditer(string[, pos[, endpos]])
搜尋string,返回一個順序訪問每一個匹配結果(Match物件)的迭代器。
import re p = re.compile(r'\d+') print(type(p.finditer('one1two2three3four4'))) for m in p.finditer('one1two2three3four4'): print(type(m)) print(m.group())結果為:
<type 'callable-iterator'>
<type '_sre.SRE_Match'>
<type '_sre.SRE_Match'>
<type '_sre.SRE_Match'>
<type '_sre.SRE_Match'>
4
解釋:
p.finditer('one1two2three3four4')是一個迭代器,而返回的每個m都是match物件,group方法也會在下一節進行詳細介紹。
6. match匹配物件
Match物件是一次匹配的結果,包含了很多關於此次匹配的資訊,可以使用Match提供的可讀屬性或方法來獲取這些資訊。上面的過程中多次使用了match物件,呼叫了他的group()和groups()等方法。
import re prog = re.compile(r'(?P<tagname>abc)(.*)(?P=tagname)') result1 = prog.match('abclfjlad234sjldabc') print(result1) print(result1.groups()) print result1.group('tagname') print(result1.group(2)) print(result1.groupdict())
返回結果:
<_sre.SRE_Match object at 0x00000000033400B8>
('abc', 'lfjlad234sjld')
abc
lfjlad234sjld
{'tagname': 'abc'}
說明:
1, 我們可以看到result1已經由字串轉換成了一個正則物件。
2, resule.groups()可以查看出來所有匹配到的資料,每個()是一個元素,最終返回一個tuple
3, group()既可以通過下標(從1開始)的方式訪問,也可以通過分組名進行訪問。
4, groupdict只能顯示有分組名的資料
group([group1, …]):
獲得一個或多個分組截獲的字串;指定多個引數時將以元組形式返回。group1可以使用編號也可以使用別名;編號0代表整個匹配的子串;不填寫引數時,返回group(0);沒有截獲字串的組返回None;截獲了多次的組返回最後一次截獲的子串。
groups([default]):
以元組形式返回全部分組截獲的字串。相當於呼叫group(1,2,…last)。default表示沒有截獲字串的組以這個值替代,預設為None。
groupdict([default]):
返回以有別名的組的別名為鍵、以該組截獲的子串為值的字典,沒有別名的組不包含在內。default含義同上。