re正則表達式方法
目錄
- 1、python正則匹配
- 1.1 re.search 正則表達式
- 1.2 re.match() 正則表達式
- 1.3 re.match與re.search的區別
- 1.4 檢索和替換
- 1.5 正則表達式對象
- 1.6 正則表達式修飾符 - 可選標誌
- 1.7 正則表達式模式
- 1.8 正則表達式實例
- 1.9 正則表達式re.findall用法
來源
1、python正則匹配
1.1 re.search 正則表達式
search將字符串的所有字串嘗試與正則表達式匹配,如果所有的字串都沒有匹配成功,返回none,否則返回matchobject;(可以理解為“無為0,有為1”)
import re s1 = "helloworld,i am 30" w1 = "world" m1 = re.search(w1,s1) if m1: print("find:%s"%m1.group())
輸出為:(m1.group()可以一次輸出多個數組--對應字符串,此處匹配到一個world,則輸出匹配到的一個字符串)
world
1.2 re.match() 正則表達式
match只從字符串的開始與正則表達式匹配,匹配成功返回matchobject,否則返回none
import re
s1 = "helloworld,i am 30"
w1 = "world"
m1 = re.match(w1,s1)
if m1:
print("find:%s"%m1.group())
else
print("no match")
輸出為:(由於re.match()從字符串開頭匹配,兩個字符串都不匹配,因此無法匹配)
no match
1.3 re.match與re.search的區別
1.4 檢索和替換
(1)repl參數是一個函數
(2)re.compile函數
(3)findall
在字符串中找到正則表達式所匹配的所有子串,並返回一個列表,如果沒有找到匹配的,則返回空列表。
註意:match和serach是匹配一次,findall是匹配所有。
語法格式為:findall(string[, pos[, endpos]])
參數:
- string:待匹配的字符串;
- pos:可選參數,指定字符串的起始位置,默認為0;
- endpos:可選參數,指定字符串的結束位置,默認為字符串的長度;
查找字符串中的所有數字:
pattern = re.complie(r‘\d+‘) #查找數字
result1 = pattern.findall(‘runoob 123 google 456‘)
result2 = pattern.findall(‘run88oobgoogle456‘, 0, 10)
print(result1)
print(result2)
輸出結果為:
[‘123‘, ‘456‘]
[‘88‘, ‘12‘]
result2 = pattern.findall(‘run88oobgoogle456‘, 0, 10)中的0指的是字符串中的起始位置,10指的是字符串的結束位置,默認為字符串的長度;
(4)re.finditer
(5)re.split
1.5 正則表達式對象
(1)re.RegexObject
(2)re.MatchObject
1.6 正則表達式修飾符 - 可選標誌
1.7 正則表達式模式
1.8 正則表達式實例
1.9 正則表達式re.findall用法
語法:findall(pattern, string, flags=0)
正則re.findall
是返回string中所有與pattern相匹配的全部字串,返回形式為數組;
(1)一般情況
regular_v1 = re.findall(r"docs", "https://docs.python.org/3/whatsnew/3.6.html")
print(regual_v1)
輸出結果為:
[‘docs‘]
(2)符號^表示匹配https開頭的字符串返回
regular_v2 = re.findall(r"^https", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v2)
輸出結果為:
[‘https‘]
(3)用$符號表示以html結尾的字符串返回,判斷是否字符串結束的字符串
regular_v3 = re.findall(r"html$", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v3)
輸出結果為:
‘html‘
(4)[...]匹配括號中的其中一個字符
regular_v4 = re.findall(r"[t,w]h", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v4)
輸出結果為:
[‘th‘, ‘wh‘]
(5)“d”是正則語法規則用來匹配0到9之間的數返回列表
regular_v5 = re.findall(r"\d", "https://docs.python.org/3/whatsnew/3.6.html")
regular_v6 = re.findall(r"\d\d\d", "https://docs.python.org/3/whatsnew/3.6.html/1234")
print(regular_v5)
print(regular_v6)
輸出結果為:
[‘3‘, ‘3‘, ‘6‘]
[‘123‘]
(6)小d表示取數字0-9,大D表示不要數字,也就是除了數字以外的內容返回
regular_v7 = re.findall(r"\D", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v7)
輸出結果為:
[‘h‘, ‘t‘, ‘t‘, ‘p‘, ‘s‘, ‘:‘, ‘/‘, ‘/‘, ‘d‘, ‘o‘, ‘c‘, ‘s‘, ‘.‘, ‘p‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘, ‘.‘, ‘o‘, ‘r‘, ‘g‘, ‘/‘, ‘/‘, ‘w‘, ‘h‘, ‘a‘, ‘t‘, ‘s‘, ‘n‘, ‘e‘, ‘w‘, ‘/‘, ‘.‘, ‘.‘, ‘h‘, ‘t‘, ‘m‘, ‘l‘]
(7)“w”在正則裏面代表匹配從小寫a到z,大寫A到Z,數字0到9
regular_v8 = re.findall(r"\w", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v8)
輸出結果為:
[‘h‘, ‘t‘, ‘t‘, ‘p‘, ‘s‘, ‘:‘, ‘/‘, ‘/‘, ‘d‘, ‘o‘, ‘c‘, ‘s‘, ‘.‘, ‘p‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘, ‘.‘, ‘o‘, ‘r‘, ‘g‘, ‘/‘, ‘/‘, ‘w‘, ‘h‘, ‘a‘, ‘t‘, ‘s‘, ‘n‘, ‘e‘, ‘w‘, ‘/‘, ‘.‘, ‘.‘, ‘h‘, ‘t‘, ‘m‘, ‘l‘]
(8)“W”在正則裏面代表匹配除了字母與數字以外的特殊符號
regular_v8 = re.findall(r"\W", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v8)
輸出結果為:
[‘:‘, ‘/‘, ‘/‘, ‘.‘, ‘.‘, ‘/‘, ‘/‘, ‘/‘, ‘.‘, ‘.‘]
re正則表達式方法