天天看點

python re子產品常用方法全總結

re.findall()

在字元串中找到正規表達式所比對的所有子串,并傳回一個清單,如果沒有找到比對的,則傳回空清單。

文法格式:

re.findall(pattern, string, flags=0)

參數:
pattern 比對字元串的正則。
string 待比對的字元串。
           

示例:

需求:查找字元串中的所有數字

result = re.findall(r'\d+','1python2hello3')
print(result)

結果
['1', '2', '3']
           
result = re.findall(r'p%','helloP%ython')
print(result)

結果
[]      #因為要比對的字元串P% 和正則p%不比對,是以傳回空


result = re.findall(r'p%','helloP%ython',re.I)
print(result)

結果
[P%]
           

re.sub()

文法:

re.sub(pattern, repl, string, count=0, flags=0)

參數:
pattern : 正則中的模式字元串。
repl : 替換的字元串,也可為一個函數。
string : 要被查找替換的原始字元串。
count : 模式比對後替換的最大次數,預設 0 表示替換所有的比對。
           
result = re.sub(r'python','world','hellopython')
print(result)

結果
helloworld
           

repl 參數是一個函數

#需求:将比對到結果中小于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 )

結果
ab88cd88ef999
           

使用場景:可以對比對成功要替換的部分做操作

re.compile

當我們在Python中使用正規表達式時,re子產品内部:

1)編譯正規表達式,如果正規表達式的字元串本身不合法,會報錯;

2)用編譯後的正規表達式去比對字元串。

那麼如果一個正規表達式要重複使用幾千次,出于效率的考慮,我們是不是應該先把這個正則先預編譯好,接下來重複使用時就不再需要編譯這個步驟了,直接比對,提高我們的效率

compile 函數用于編譯正規表達式,生成一個正規表達式( Pattern )對象,供 match() 和 search() 這兩個函數使用。

文法格式為:

re.compile(pattern[, flags])

參數:
pattern : 一個字元串形式的正規表達式
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)

結果
ab12
('ab', '12')
           

re.match

re.match 嘗試從字元串的起始位置比對一個模式,如果不是起始位置比對成功的話,match()就傳回none。

文法

re.match(pattern, string, flags=0)

參數
pattern     比對的正規表達式
string      要比對的字元串。
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)

結果
ab12
('ab', '12')



str1 = '0b12cd34ef567'
result1 = info.match(str1).group()
print(result1)

結果: 會報錯
AttributeError: 'NoneType' object has no attribute 'group'
           

re.search

re.search 掃描整個字元串并傳回第一個成功的比對。

參數
pattern     比對的正規表達式
string      要比對的字元串。
flags       标志位,用于控制正規表達式的比對方式,如:是否區分大小寫,多行比對等
           
str = 'ab12cd34ef567'
info = re.compile(r'([0-9]{2})')
result = info.search(str).group()
print(result)

結果
12
           

re.match與re.search的差別

re.match隻比對字元串的開始,如果字元串開始不符合正規表達式,則比對失敗,函數傳回None;而re.search比對整個字元串,直到找到一個比對。

re.split

split 方法按照能夠比對的子串将字元串分割後傳回清單

文法

re.split(pattern, string[, maxsplit=0, flags=0])

參數
pattern     比對的正規表達式
string      要比對的字元串。
flags       标志位,用于控制正規表達式的比對方式,如:是否區分大小寫,多行比對等
           
str = 'abc def g hi'
result = re.split('\W+',str)
print(result)

結果
['abc', 'def', 'g', 'hi']