Python-正則對象的方法

re 模塊使Python 語言擁有全部的正則表達式功能

complile函數根據一個模式字符串和可選的標誌函數生成一個正則表達式對象。該對象擁有一系列方法用於正則表達式匹配和替換。

1.match 方法

rematch 嘗試從字符串的起始位置匹配一個模式,如果不是起始位置匹配成的話,match() 返回None

匹配成果返回一個匹配的對象。

語法:

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

string:匹配使用的文本,

pos: 文本中正則表達式開始搜索的索引。及開始搜索string的下標

endpos: 文本中正則表達式結束搜索的索引。

如果不指定pos,默認是從開頭開始匹配,如果匹配不到,直接返回None

匹配對象方法描述group(num=0)匹配的整個表達式的字符串,group() 可以一次輸入多個組號,在這種情況下它將返回一個包含那些組所對應值的元組。groups()返回一個包含所有小組字符串的元組,從 1 到 所含的小組號。

使用group()、groups()匹配對象函數來獲取匹配表達式.

import re
print(re.match('www','www.runoob.com').span())
print(re.match('com','www.runoob.com'))
(0, 3)
None
result = pattern.match(r'aahello world hello ling')
print(result)
result2 = pattern.match(r'hello world hello ling')
print(result2.groups())
<_sre.SRE_Match object at 0x00000000025450B8>
('hello world ', 'hello ling')
解釋:如果不指定pos的話,默認是從字符串開始位置匹配,匹配不到就返回None,以上所有的pattern都是一個match對象,


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

match  ()方法默認從開始匹配,若匹配不到直接返回none。

search()方法 從開始位置匹配,若匹配不到,下標加一,直到最後。


3. split 方法

split(string[, maxsplit])

按照能夠匹配的子串將string分割後返回列表。maxsplit用於指定最大分割次數,不指定將全部分割
import re
p = re.compile(r'\d+')
print(p.split('one1two2three3four4'))

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

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



4.findall 方法

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

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

import re
p = re.compile(r'\d+')
print(p.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對象

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 0x0000000002585140>
('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含義同上。
















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