20171115_Python学习五周三次课
今日任務:
五周三次課(11月15日)
11.4 re的matche方法和search方法
11.5 re的split,findall,finditer方法
11.6 re的match對象
?
match方法
match(string[, pos[, endpos]])
string:匹配使用的文本,
pos: 文本中正則表達式開始搜索的索引。及開始搜索string的下標
endpos: 文本中正則表達式結束搜索的索引。
如果不指定pos,默認是從開頭開始匹配,如果匹配不到,直接返回None
import re
pattern = re.compile(r'\w*(hello w.*)(hello l.*)')
result = pattern.match(r'aahello world hello ling')
print(result)
result2 = pattern.match(r'hello world hello ling')
print(result2.groups())
結果:
None
('hello world ', 'hello ling')
解釋:如果不指定pos的話,默認是從字符串開始位置匹配,匹配不到就返回None,以上所有的pattern都是一個match對象。
search方法
search(string[, pos[, endpos]])
這個方法用于查找字符串中可以匹配成功的子串。從string的pos下標處起嘗試匹配pattern,如果pattern結束時仍可匹配,則返回一個Match對象;若無法匹配,則將pos加1后重新嘗試匹配;直到pos=endpos時仍無法匹配則返回None。下面看個列子:
import re
pattern = re.compile(r'(hello w.*)(hello l.*)')
result1 = pattern.search(r'aahello world hello ling')
print(result1.groups())
結果:
('hello world ', 'hello ling')
解釋:
split方法
split(string[, maxsplit])
按照能夠匹配的子串將string分割后返回列表。maxsplit用于指定最大分割次數,不指定將全部分割。
import re
p = re.compile(r'\d+')
print(p.split('one1two2three3four4'))
結果:
['one', 'two', 'three', 'four', '']
解釋:直接把p的正則當成是分隔符,然后把最后的字符串用p進行分割,然后返回回去
findall方法
findall(string[, pos[, endpos]])?
搜索string,以列表形式返回全部能匹配的子串.
import re
p = re.compile(r'\d+')
print(findall('one1two2three3four4'))
結果:
['1', '2', '3', '4']
結果:findall是把匹配到的字符串最后一列表的形式返回回去
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'>
1
<type '_sre.SRE_Match'>
2
<type '_sre.SRE_Match'>
3
<type '_sre.SRE_Match'>
4
解釋:
p.finditer('one1two2three3four4')是一個迭代器,而返回的每個m都是match對象
sub方法
sub(repl, string[, count])?
使用repl替換string中每一個匹配的子串后返回替換后的字符串。
當repl是一個字符串時,可以使用\id或\g<id>、\g<name>引用分組,但不能使用編號0。
當repl是一個方法時,這個方法應當只接受一個參數(Match對象),并返回一個字符串用于替換(返回的字符串中不能再引用分組)。
count用于指定最多替換次數,不指定時全部替換。
import re
p = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!'
print(p.sub(r'\2 \1', s))
def func(m):
??? return m.group(1).title() + ' ' + m.group(2).title()
print(p.sub(func, s))
結果:
say i, world hello!
I Say, Hello World!
解釋:
\(id)就是匹配的括號的內容,id從默認從1開始計數
m.group(1)是一個字符串,調用字符串的title()方法,所有單詞的搜字母大寫。
?
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 0x0000000002176E88>
('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含義同上。
?
轉載于:https://www.cnblogs.com/zhuntidaoren/p/7841250.html
總結
以上是生活随笔為你收集整理的20171115_Python学习五周三次课的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: axure rp 8.0
- 下一篇: libevent的vs2013的源码工程