06Python爬蟲---正則表達式04之常用表達式

常用的正則表達式函數
re.match()函數、re.search()函數、全局匹配函數、re.sub()函數

一、re.match()函數

從源字符串的起始位置匹配一個模式
格式re.match(pattern, string, flag)
第一個參數代表對應的正則表達式,第二個參數代表對應的源字符,第三個參數是可選參數,代表對應的標誌位,可以放模式修正符等信息

import re
string = "apythonhellomypythonhispythonourpythonend"
pattern = ".python."
result = re.match(pattern, string)
result1 = re.match(pattern, string).span()
print("結果1:%s" % result)  # 結果1:<_sre.SRE_Match object; span=(0, 8), match='apythonh'>
print(result1)  # (0,8)

二、re.search()函數

掃描整個字符串進行匹配
格式re.search(pattern, string, flag)

import re
string = "hellomypythonhis"
pattern = ".python."
result = re.match(pattern, string)
result1 = re.search(pattern, string)
print("結果1:%s" % result)  # 結果1:None
print("結果2:%s" % result1)  # 結果2:<_sre.SRE_Match object; span=(0, 8), match='ypythonh'>

三、全局匹配函數

將符合模式的全部內容都匹配出來

思路
(1)使用re.compile()對正則表達式進行預編譯
(2)編譯後使用findall()根據正則表達式從原字符串中將匹配的解決全部找出。

import re
string = "hellomypythonhispythonourpythonend"
pattern = re.compile(".python.")  # 預編譯
result = pattern.findall(string)  # 找出符合模式的所有結果
print("全局匹配結果:%s" % result)  # 全局匹配結果:['ypythonh', 'spythono', 'rpythone']

整合

import re
string = "hellomypythonhispythonourpythonend"
pattern = ".python."
result = re.compile(pattern).findall(string)
print("全局匹配結果:%s" % result)  # 全局匹配結果:['ypythonh', 'spythono', 'rpythone']

四、re.sub()函數

根據正則表達式來實現替換某些字符串
re.sub(pattern, rep, string, max)
第一個參數對應的正則表達式,第二個參數爲要替換成的字符串,第三個參數爲源字符串,第四個參數爲可選項,代表最多替換的次數,如果忽略不寫,則會將符合模式的結果全部替換。

import re
string = "hellomypythonhispythonourpythonend"
pattern = "python."
result1 = re.sub(pattern, "php", string)  # 全部替換
result2 = re.sub(pattern, "php", string, 2)  # 最多替換2次
print("結果1:%s" % result1)  # 結果1:hellomyphpisphpurphpnd
print("結果2:%s" % result2)  # 結果2:hellomyphpisphpurpythonend
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章