這篇文章主要介紹Python正則表達(dá)式怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)堅(jiān)信:善待客戶,將會(huì)成為終身客戶。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨嚒N覀儚牟缓鲇瞥踉L客戶,我們用心做好本職工作,不忘初心,方得始終。十載網(wǎng)站建設(shè)經(jīng)驗(yàn)成都創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營(yíng)銷服務(wù)商,為您提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、H5響應(yīng)式網(wǎng)站、網(wǎng)站制作、成都品牌網(wǎng)站建設(shè)、微信小程序服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。
正則表達(dá)式是處理字符串的強(qiáng)大工具。作為一個(gè)概念而言,正則表達(dá)式對(duì)于Python來說并不是獨(dú)有的。但是,Python中的正則表達(dá)式在實(shí)際使用過程中還是有一些細(xì)小的差別。
正則表達(dá)式是一個(gè)特殊的字符序列,它能幫助你方便的檢查一個(gè)字符串是否與某種模式匹配。
Python 自1.5版本起增加了re 模塊,它提供 Perl 風(fēng)格的正則表達(dá)式模式。
re 模塊使 Python 語言擁有全部的正則表達(dá)式功能。
compile 函數(shù)根據(jù)一個(gè)模式字符串和可選的標(biāo)志參數(shù)生成一個(gè)正則表達(dá)式對(duì)象。該對(duì)象擁有一系列方法用于正則表達(dá)式匹配和替換。
re 模塊也提供了與這些方法功能完全一致的函數(shù),這些函數(shù)使用一個(gè)模式字符串做為它們的第一個(gè)參數(shù)。
1、查找第一個(gè)匹配串
import re s='i love python very much' pat='python' r=re.search(pat,s) print(r.span())#(7,13)
2、查找所有1
import re s='山東省濰坊市青州第1中學(xué)高三1班' pat='1' r=re.finditer(pat,s) for i in r: print(i) # <re.Match object; span=(9, 10), match='1'> # <re.Match object; span=(14, 15), match='1'>
3、\d匹配數(shù)字[0-9]
import re s='一共20行代碼運(yùn)行時(shí)間13.59s' pat=r'\d+'#+表示匹配數(shù)字(\d表示數(shù)字的通用字符)1次或多次 r=re.findall(pat,s) print(r) #['20','13','59']
我們想保留13.59而不是分開,請(qǐng)看4
4、?表示前一個(gè)字符匹配0或1次
import re s='一共20行代碼運(yùn)行時(shí)間13.59s' pat=r'\d+\.?\d+'#?表示匹配小數(shù)點(diǎn)(\.)0次或1次 r=re.findall(pat,s) print(r) #['20','13.59']
5、^匹配字符串的開頭
import re s='This module provides regular expression matching operations similar to those found in Perl' pat=r'^[emrt]' #查找以 r=re.findall(pat,s) print(r) # [],因?yàn)樽址拈_頭是字符`T`,不在emrt匹配范圍內(nèi),所以返回為空
6、re.I 忽略大小寫
import re s='This module provides regular expression matching operations similar to those found in Perl' pat=r'^[emrt]' #查找以 r=re.compile(pat,re.I).search(s) print(r) # <re.Match object; span=(0, 1), match='T'> 表明字符串的開頭在匹配列表中
7、使用正則提取單詞
這是不準(zhǔn)確版本,請(qǐng)參看第9個(gè)
import re s='This module provides regular expression matching operations similar to those found in Perl' pat=r'\s[a-zA-Z]+' r=re.findall(pat,s) print(r) #[' module', ' provides', ' regular', ' expression', ' matching', ' operations', ' similar', ' to', ' those', ' found', ' in', ' Perl']
8、只捕獲單詞,去掉空格
使用()捕獲,這是不準(zhǔn)確版本,請(qǐng)參看第9個(gè)
import re s='This module provides regular expression matching operations similar to those found in Perl' pat=r'\s([a-zA-Z]+)' r=re.findall(pat,s) print(r) #['module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
9、補(bǔ)充上第一個(gè)單詞
上面第8,看到提取單詞中未包括第一個(gè)單詞,使用?表示前面字符出現(xiàn)0次或1次,但是此字符還有表示貪心或非貪心匹配含義,使用時(shí)要謹(jǐn)慎。
import re s='This module provides regular expression matching operations similar to those found in Perl' pat=r'\s?([a-zA-Z]+)' r=re.findall(pat,s) print(r) #['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
10、使用split函數(shù)直接分割單詞
使用以上方法分割單詞,不是簡(jiǎn)潔的,僅僅為了演示。分割單詞最簡(jiǎn)單還是使用split函數(shù)。
import re s = 'This module provides regular expression matching operations similar to those found in Perl' pat = r'\s+' r = re.split(pat,s) print(r) #['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl']
11、提取以m或t開頭的單詞,忽略大小寫
下面出現(xiàn)的結(jié)果不是我們想要的,原因出在 ?上!
import re s='This module provides regular expression matching operations similar to those found in Perl' pat=r'\s?([mt][a-zA-Z]*)' # 查找以 r=re.findall(pat,s) print(r) #['module', 'matching', 'tions', 'milar', 'to', 'those']
12、使用^查找字符串開頭的單詞
綜合11和12得到所有以m或t開頭的單詞
import re s='This module provides regular expression matching operations similar to those found in Perl' pat=r'^([mt][a-zA-Z]*)\s' # 查找以 r=re.compile(pat,re.I).findall(s) print(r) #['This']
13、先分割,再查找滿足要求的單詞
使用match表示是否匹配
import re s='This module provides regular expression matching operations similar to those found in Perl' pat=r'\s+' r=re.split(pat,s) res=[i for i in r if re.match(r'[mMtT]',i)] print(res) #['This', 'module', 'matching', 'to', 'those']
14、貪心匹配
盡可能多的匹配字符
import re content='<h>ddedadsad</h><div>graph</div>bb<div>math</div>cc' pat=re.compile(r"<div>(.*)</div>") #貪婪模式 m=pat.findall(content) print(m) #['graph</div>bb<div>math']
15、非貪心匹配
與14相比,僅僅多了一個(gè)問號(hào)(?),得到結(jié)果完全不同。
import re content='<h>ddedadsad</h><div>graph</div>bb<div>math</div>cc' pat=re.compile(r"<div>(.*?)</div>") #貪婪模式 m=pat.findall(content) print(m) #['graph', 'math']
與14比較可知,貪心匹配和非貪心匹配的區(qū)別,后者是字符串匹配后立即返回,見好就收。
16、含有多種分割符
使用split函數(shù)
import re content = 'graph math,,english;chemistry' #這種 pat=re.compile(r"[\s\,\;]+") #貪婪模式 m=pat.split(content) print(m) #['graph', 'math', 'english', 'chemistry']
17、替換匹配的子串
sub函數(shù)實(shí)現(xiàn)對(duì)匹配子串的替換
import re content="hello 12345, hello 456321" pat=re.compile(r'\d+') #要替換的部分 m=pat.sub("666",content) print(m) #hello 666, hello 666
18、爬取百度首頁標(biāo)題
import re from urllib import request #爬蟲爬取百度首頁內(nèi)容 data=request.urlopen("http://www.baidu.com/").read().decode() #分析網(wǎng)頁,確定正則表達(dá)式 pat=r'<title>(.*?)</title>' result=re.search(pat,data) print(result) #<re.Match object; span=(1389, 1413), match='<title>百度一下,你就知道</title>'>
下面是知識(shí)點(diǎn)分享
19、常用元字符總結(jié)
. 匹配任意字符
^ 匹配字符串始位置
$ 匹配字符串中結(jié)束的位置
* 前面的原子重復(fù)0次1次多次
? 前面的原子重復(fù)一次或者0次
+ 前面的原子重復(fù)一次或多次
{n} 前面的原子出現(xiàn)了 n 次
{n,} 前面的原子至少出現(xiàn) n 次
{n,m} 前面的原子出現(xiàn)次數(shù)介于 n-m 之間
( ) 分組,需要輸出的部分
20、常用通用字符總結(jié)
\s 匹配空白字符
\w 匹配任意字母/數(shù)字/下劃線
\W 和小寫 w 相反,匹配任意字母/數(shù)字/下劃線以外的字符
\d 匹配十進(jìn)制數(shù)字
\D 匹配除了十進(jìn)制數(shù)以外的值
[0-9] 匹配一個(gè)0-9之間的數(shù)字
[a-z] 匹配小寫英文字母
[A-Z] 匹配大寫英文字母
以上是“Python正則表達(dá)式怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文題目:Python正則表達(dá)式怎么用
當(dāng)前路徑:http://bm7419.com/article2/goehic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、自適應(yīng)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、軟件開發(fā)、企業(yè)網(wǎng)站制作、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)