Python中re模塊常用方法總結(jié)分析
re.findall()
在字符串中找到正則表達(dá)式所匹配的所有子串,并返回一個列表,如果沒有找到匹配的,則返回空列表。
語法格式:
re.findall(pattern, string, flags=0)
參數(shù):pattern:匹配字符串的正則。string :待匹配的字符串。
示例:
需求:查找字符串中的所有數(shù)字
result = re.findall(r'\d+','1python2hello3') print(result)
結(jié)果
['1', '2', '3']
result = re.findall(r'p%','helloP%ython') print(result)
結(jié)果
[] #因為要匹配的字符串P% 和正則p%不匹配,所以返回空
result = re.findall(r'p%','helloP%ython',re.I) print(result)
結(jié)果
[P%]
re.sub()
語法:
re.sub(pattern, repl, string, count=0, flags=0)
參數(shù):pattern : 正則中的模式字符串。repl : 替換的字符串,也可為一個函數(shù)。string : 要被查找替換的原始字符串。count : 模式匹配后替換的最大次數(shù),默認(rèn) 0 表示替換所有的匹配。
result = re.sub(r'python','world','hellopython') print(result)
結(jié)果
helloworld
repl 參數(shù)是一個函數(shù)
#需求:將匹配到結(jié)果中小于4的替換成8,大于4的替換成9
import re
def modify(value):
matched = value.group()
if int(matched) <= 4:
return '8'
else:
return '9'
str = 'ab12cd34ef567'
result = re.sub('\d', modify, str)
print(result )
結(jié)果
ab88cd88ef999
使用場景:可以對匹配成功要替換的部分做操作
re.compile
當(dāng)我們在Python中使用正則表達(dá)式時,re模塊內(nèi)部:
- 1)編譯正則表達(dá)式,如果正則表達(dá)式的字符串本身不合法,會報錯;
- 2)用編譯后的正則表達(dá)式去匹配字符串。
那么如果一個正則表達(dá)式要重復(fù)使用幾千次,出于效率的考慮,我們是不是應(yīng)該先把這個正則先預(yù)編譯好,接下來重復(fù)使用時就不再需要編譯這個步驟了,直接匹配,提高我們的效率
compile 函數(shù)用于編譯正則表達(dá)式,生成一個正則表達(dá)式( Pattern )對象,供 match() 和 search() 這兩個函數(shù)使用。
語法格式為:
re.compile(pattern[, flags])
參數(shù):pattern : 一個字符串形式的正則表達(dá)式flags : 可選,表示匹配模式,比如忽略大小寫,多行模式等,
str = 'ab12cd34ef567'
info = re.compile(r'([a-z]{2})(\d{2})')
result = info.match(str).group()
result1 = info.match(str).groups()
print(result)
print(result1)
結(jié)果
ab12
('ab', '12')
re.match
re.match 嘗試從字符串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就返回none。
語法
re.match(pattern, string, flags=0)
參數(shù):pattern:匹配的正則表達(dá)式string:要匹配的字符串。flags: 標(biāo)志位,用于控制正則表達(dá)式的匹配方式,如:是否區(qū)分大小寫,多行匹配等
str = 'ab12cd34ef567'
info = re.compile(r'([a-z]{2})(\d{2})')
result = info.match(str).group()
result1 = info.match(str).groups()
print(result)
print(result1)
結(jié)果
ab12
('ab', '12')
str1 = '0b12cd34ef567' result1 = info.match(str1).group() print(result1)
結(jié)果: 會報錯
AttributeError: 'NoneType' object has no attribute 'group'
re.search
re.search 掃描整個字符串并返回第一個成功的匹配。
參數(shù):pattern:匹配的正則表達(dá)式string:要匹配的字符串。flags:標(biāo)志位,用于控制正則表達(dá)式的匹配方式,如:是否區(qū)分大小寫,多行匹配等
str = 'ab12cd34ef567'
info = re.compile(r'([0-9]{2})')
result = info.search(str).group()
print(result)
結(jié)果
12
re.match與re.search的區(qū)別
re.match只匹配字符串的開始,如果字符串開始不符合正則表達(dá)式,則匹配失敗,函數(shù)返回None;
re.search匹配整個字符串,直到找到一個匹配。
re.split
split 方法按照能夠匹配的子串將字符串分割后返回列表
語法
re.split(pattern, string[, maxsplit=0, flags=0])
參數(shù):pattern: 匹配的正則表達(dá)式string:要匹配的字符串。flags:標(biāo)志位,用于控制正則表達(dá)式的匹配方式,如:是否區(qū)分大小寫,多行匹配等
str = 'abc def g hi'
result = re.split('\W+',str)
print(result)
結(jié)果
['abc', 'def', 'g', 'hi']
以上就是Python中re模塊常用方法總結(jié)分析的詳細(xì)內(nèi)容,更多關(guān)于Python中re模塊常用方法的資料請關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信