一、re模塊介紹
Python提供了一個內置模塊re,它提供了正則表達式(regular expression)操作的功能。正則表達式是一種表達特定模式的方法,通過一個模式字元串來匹配文本中的符合條件的子字元串。
要使用re模塊,需要先導入它:
import re
在使用re模塊之前,先了解一下一些最基礎的正則表達式元字元:
- . :匹配除換行符以外的任意字元
- ^ :匹配字元串的開頭
- $ :匹配字元串的結尾
- * :匹配前面的子表達式零次或多次
- ? :匹配前面的子表達式零次或一次
- + :匹配前面的子表達式一次或多次
- {n} :匹配前面的子表達式恰好n次,n為非負整數
- {n,} :匹配前面的子表達式至少n次
- {n,m} :匹配前面的子表達式至少n次,至多m次
- [] :匹配中括弧中的任意一個字元
- | :匹配兩個或多個規則之一
- () :標記一個子表達式的開始和結束位置,並保存匹配的結果
二、re.match方法
re.match方法從字元串的開頭開始查找,如果找到符合正則表達式規則的子字元串,就返回一個匹配對象,否則返回None。
match函數的語法格式如下:
m = re.match(pattern, string, flags=0)
其中,pattern代表正則表達式的模式字元串,string是要匹配的字元串,flags是匹配模式(一般使用默認值即可)。
例如:
pattern = r'hello'
string = 'hello world!'
m = re.match(pattern, string)
print(m)
執行結果為:
<re.Match object; span=(0, 5), match='hello'>
其中,span是匹配結果的起始位置和結束位置,match是匹配的子字元串。
三、re.search方法
re.search方法從整個字元串中查找,如果找到符合正則表達式規則的子字元串,就返回一個匹配對象,否則返回None。
search函數的語法格式如下:
m = re.search(pattern, string, flags=0)
例如:
pattern = r'world'
string = 'hello world!'
m = re.search(pattern, string)
print(m)
執行結果為:
<re.Match object; span=(6, 11), match='world'>
同樣,span是匹配結果的起始位置和結束位置,match是匹配的子字元串。
四、re.findall方法
re.findall方法會搜索整個字元串,以列表形式返回所有符合正則表達式規則的子字元串。
findall函數的語法格式如下:
result = re.findall(pattern, string, flags=0)
例如:
pattern = r'\d+'
string = '12 drummers drumming, 11 pipers piping, 10 lords a-leaping'
result = re.findall(pattern, string)
print(result)
執行結果為:
['12', '11', '10']
注意到正則表達式中的\d代表任意一個數字,+代表前面的子表達式至少要出現一次。
五、re.sub方法
re.sub方法將字元串中符合正則表達式規則的子字元串替換成新的字元串。
sub函數的語法格式如下:
new_string = re.sub(pattern, replace, string, count=0, flags=0)
其中,pattern代表正則表達式的模式字元串,replace是替換的新字元串,string是要匹配的字元串,count是可選參數,表示要替換的最大數量,flags是匹配模式(一般使用默認值即可)。
例如:
pattern = r'\s+'
replace = ','
string = 'hello world!'
new_string = re.sub(pattern, replace, string)
print(new_string)
執行結果為:
hello,world!
注意到正則表達式中的\s代表任意一個空白字元,+代表前面的子表達式至少要出現一次。
六、re.compile方法
re.compile方法可以將正則表達式的模式字元串編譯為一個正則表達式對象,可以多次使用。
compile函數的語法格式如下:
p = re.compile(pattern, flags=0)
其中,pattern代表正則表達式的模式字元串,flags是匹配模式(一般使用默認值即可)。
例如:
pattern = r'\d+'
string1 = '12 drummers drumming, 11 pipers piping, 10 lords a-leaping'
string2 = 'no numbers'
p = re.compile(pattern)
result1 = p.findall(string1)
result2 = p.findall(string2)
print(result1,result2)
執行結果為:
(['12', '11', '10'], [])
注意到正則表達式對象p可以根據pattern多次使用。
七、總結
本文介紹了Python中re模塊的基本用法,涉及了match、search、findall、sub和compile等方法。正則表達式是一種強大的文本匹配工具,掌握它可以提高代碼的處理速度和效率,適合需要經常處理字元串的工程師。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/291973.html