網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > 學習教育 > 

一文秒懂python正則表達式常用函數

欄目: 學習教育 / 發佈於: / 人氣:4.87K

導讀: 正則表達式是處理字符串類型的"核武器",不僅速度快,而且功能強大。本文不過多展開正則表達式相關語法,僅簡要 介紹 python中正則表達式常用函數及其使用方 法,以作快速查詢瀏覽。

一文秒懂python正則表達式常用函數

01 Re概覽

Re模塊是python的內置模塊,提供了正則表達式在python中的所有用法,默認安裝位置在python根目錄下的Lib文件夾(如 ..PythonPython37Lib)。主要提供了3大類字符串操作方法:

字符查找/匹配

字符替換

字符分割

由於是面向字符串類型的模塊,就不得不提到字符串編碼類型。re模塊中,模式串和搜索串既可以是 Unicode 字符串 (常用str類型) ,也可以是8位字節串 (bytes,2位16進制數字,例如xe5) , 但要求二者必須是同類型字符串。

02 字符串查找/匹配

預編譯:compile

在介紹查找和匹配函數前,首先需要知道re的compile函數,該函數可以將一個模式串編譯成正則表達式類型,以便後續快速匹配和複用

import repattern = re.compile(r'[a-z]{2,5}')type(pattern) #re.Pattern

此例創建了一個正則表達式式對象 (re.pattern) ,命名爲pattern,用於匹配2-5位小寫字母的模式串。後續在使用其他正則表達式函數時,即可使用pattern進行方法調用。

匹配:match

match函數用於從文本串的起始位置開始匹配,若匹配成功,則返回相應的匹配對象,此時可調用group()方法返回匹配結果,也可用span()方法返回匹配起止下標區間;否則返回None

import repattern = re.compile(r'[a-z]{2,5}')text1 = 'this is a re test'res = pattern.match(text1)print(res) #<re.Match object; span=(0, 4), match='this'>if res: print(res.group()) #this print(res.span()) #(0, 4)text2 = '是的, this is a re test'print(pattern.match(text2))#None

match函數還有一個變形函數fullmatch,當且僅當模式串與文本串剛好全部匹配時,返回一個匹配對象,否則返回None

搜索:search

match只提供了從文本串起始位置匹配的結果,如果想從任意位置匹配,則可調用search方法,與match方法類似,當任意位置匹配成功,則立即返回一個匹配對象,也可調用span()方法獲取起止區間、調用group方法獲得匹配文本串

import repattern = re.compile(r's[a-z]{2}')text1 = 'this is a re test'res = pattern.search(text1)print(res) #<re.Match object; span=(4, 7), match=' is'>if res: print(res.group()) #is print(res.span()) #(4, 7)pattern2 = re.compile(r's[a-z]{5}')text2 = '是的,this is a re test'print(pattern2.search(text2))#None

match和search均用於匹配單個結果,唯一區別在於前者是從起始位置開始匹配,而後者從任意位置匹配,匹配成功則返回一個match對象。

全搜索:findall/finditer

幾乎是最常用的正則表達式函數,用於尋找所有匹配的結果,例如在爬蟲信息提取中,可非常方便地提取所有匹配字段

import repattern = re.compile(r's[a-z]{2,5}')text1 = 'this is a re test'res = pattern.findall(text1)print(res) #[' is', ' re', ' test']

findall返回的是一個列表對象類型,當無匹配對象時,返回一個空列表。爲了避免因同時返回大量匹配結果佔用過多內存,可以調用finditer函數返回一個迭代器類型,其中每個迭代元素是一個match對象,可繼續調用group和span方法獲取相應結果

import repattern = re.compile(r's[a-z]{2,5}')text1 = 'this is a re test'res = pattern.finditer(text1)for r in res: print(r.group())""" is re test"""

當匹配模式串較爲簡單或者僅需單詞調用時,上述所有方法也可直接調用re類函數,而無需事先編譯。此時各方法的第一個參數爲模式串。

import repattern = re.compile(r'd{2,5}')text = 'this is re test're.findall('[a-z]+', text) #['this', 'is', 're', 'test']

03 字符串替換/分割

替換:sub/subn

當需要對文本串進行條件替換時,可調用re.sub實現 (當然也可先編譯後再用調用實例方法) ,相應參數分別爲模式串、替換格式、文本串,還可以通過增加缺省參數限定替換次數和匹配模式。通過在模式串進行分組,可實現字符串的格式化替換(類似字符串的format方法),以實現特定任務。

import retext = 'today is 2020-03-05'print(re.sub('-', '', text)) #'today is 20200305'print(re.sub('-', '', text, 1)) #'today is 202003-05'print(re.sub('(d{4})-(d{2})-(d{2})', r'2/3/1', text)) #'today is 03/05/2020'

re.sub的一個變形方法是re.subn,區別是返回一個2元素的元組,其中第一個元素爲替換結果,第二個爲替換次數

import retext = 'today is 2020-03-05'print(re.subn('-', '', text)) #('today is 20200305', 2)

分割:split

還可以調用正則表達式實現字符串的特定分割,相當於.split()方法的一個加強版,實現特定模式的分割,返回一個切割後的結果列表

import retext = 'today is a re test, what do you mind?'print(re.split(',', text)) #['today is a re test', ' what do you mind?']

04 總結

python中的re模塊提供了正則表達式的常用方法,每種方法都包括類方法調用(如re.match)或模式串的實例調用(pattern.match)2種形式

常用的匹配函數:match/fullmatch

常用的搜索函數:search/findall/finditer

常用的替換函數:sub/subn

常用的切割函數:split

還有其他很多方法,但不是很常用,具體可參考官方文檔

另外,python還有第三方正則表達式庫regex可供選擇

到此這篇關於一文秒懂python正則表達式常用函數的文章就介紹到這了,希望大家以後多多支持好二三四!

<link rel="stylesheet" href="https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da5ee921b51c/da42d322a20a.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da5ee921b51c/da42c425b502dd685f0fcdad1b74.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d824b707d4455d01d8bd05.js"></script> <script>SyntaxHighlighter.autoloader( 'applescript https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06d4a19c0bd24631b48642f.js', 'actionscript3 as3 https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06d695a.js', 'bash shell https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06e5b1ac4.js', 'coldfusion cf https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06f5505c89e0273004e7a.js', 'cpp c https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06f4a19.js', 'obj-c objc https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0635803ef.js', 'c# c-sharp csharp https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06f6901cdaa07.js', 'css https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06f491a.js', 'delphi pascal https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0685f05dcb01e.js', 'diff patch pas https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd068530fca.js', 'erl erlang https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0694805cdb610.js', 'groovy https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06b4806c3ae0e.js', 'haxe hx https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0645b11c9.js', 'java https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0665b1fcd.js', 'jfx javafx https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0665b1fcd9e2f.js', 'js jscript javascript https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd066690adeb10774.js', 'perl pl https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c5f1bc0.js', 'php https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c5219.js', 'text plain https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c5608c5b6.js', 'py python https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c431dc4b719.js', 'ruby rails ror rb https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07e4f0bd5.js', 'scala https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07f5908c0b9.js', 'sql https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07f4b05.js', 'vb vbnet https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07a58.js', 'ps powershell https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd07c551ec9aa24680c4d78.js', 'xml xhtml xslt html https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd0745705.js', 'go golang https://js.how234.com/c359fc24b2/da53fe39b117d0455d01c0b110681d4466/da49e224a01bcb1e0a5b9e/da42d23fa51cd06b55.js' );</script> <script type="text/javascript"> SyntaxHighlighter.all(); </script>