學習筆記(11月15日)

一、 re的matche方法和search方法

1.1match方法

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.group(0))
result2 = pattern.match(r'hello world hello ling')
print(result2.groups())

結果:

aahello world hello ling
('hello world ', 'hello ling')

解釋:

如果不指定pos的話,默認是從字符串開始位置匹配,匹配不到就返回None,以上所有的pattern都是一個match對象,他在查找結果的時候有自己的方法,我們在後一節會詳細解釋他的方法。


2.2search方法

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')


二、 re的split,findall,finditer方法

2.1正則對象的split方法

split(string[, maxsplit])

按照能夠匹配的子串將string分割後返回列表。maxsplit用於指定最大分割次數,不指定將全部分割。

import re
p = re.compile(r'\d+')
print(p.split('one1two2three3four4'))

結果:

['one', 'two', 'three', 'four', '']

解釋:

直接把p的正則當成是分隔符,然後把最後的字符串用p進行分割,然後返回回去


2.2正則對象的findall方法

findall(string[, pos[, endpos]]) 

搜索string,以列表形式返回全部能匹配的子串。

import re
p = re.compile(r'\d+')
print(p.findall('one1two2three3four4'))

結果:

['1', '2', '3', '4']

解釋:

findall是把匹配到的字符串以列表的形式返回回去


2.3正則對象的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對象,group方法會在下一節進行詳細介紹。


2.4正則對象的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()方法,所有單詞的首字母大寫。


三、 re的match對象

3.1match匹配對象

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 0x01E66CC8>
('abc', 'lfjlad234sjld')
abc
lfjlad234sjld
{'tagname': 'abc'}

解釋:

1,我們可以看到result1已經由字符串轉換成了一個正則對象。

2,resule1.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含義同上。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章