Python中正則表達(dá)式的用法實(shí)例匯總
正則表達(dá)式是Python程序設(shè)計(jì)中非常實(shí)用的功能,本文就常用的正則表達(dá)式做一匯總,供大家參考之用。具體如下:
一、字符串替換
1.替換所有匹配的子串
用newstring替換subject中所有與正則表達(dá)式regex匹配的子串
result, number = re.subn(regex, newstring, subject)
2.替換所有匹配的子串(使用正則表達(dá)式對(duì)象)
reobj = re.compile(regex) result, number = reobj.subn(newstring, subject)
二、字符串拆分
1.字符串拆分
result = re.split(regex, subject)
2.字符串拆分(使用正則表示式對(duì)象)
reobj = re.compile(regex) result = reobj.split(subject)
三、匹配
下面列出Python正則表達(dá)式的幾種匹配用法:
1.測(cè)試正則表達(dá)式是否匹配字符串的全部或部分
regex=ur"..." #正則表達(dá)式 if re.search(regex, subject): do_something() else: do_anotherthing()
2.測(cè)試正則表達(dá)式是否匹配整個(gè)字符串
regex=ur"...\Z" #正則表達(dá)式末尾以\Z結(jié)束 if re.match(regex, subject): do_something() else: do_anotherthing()
3. 創(chuàng)建一個(gè)匹配對(duì)象,然后通過(guò)該對(duì)象獲得匹配細(xì)節(jié)
regex=ur"..." #正則表達(dá)式 match = re.search(regex, subject) if match: # match start: match.start() # match end (exclusive): match.end() # matched text: match.group() do_something() else: do_anotherthing()
4.獲取正則表達(dá)式所匹配的子串
(Get the part of a string matched by the regex) regex=ur"..." #正則表達(dá)式 match = re.search(regex, subject) if match: result = match.group() else: result = ""
5. 獲取捕獲組所匹配的子串
(Get the part of a string matched by a capturing group) regex=ur"..." #正則表達(dá)式 match = re.search(regex, subject) if match: result = match.group(1) else: result = ""
6. 獲取有名組所匹配的子串
(Get the part of a string matched by a named group)
regex=ur"..." #正則表達(dá)式
match = re.search(regex, subject)
if match:
result = match.group("groupname")
else:
result = ""
7. 將字符串中所有匹配的子串放入數(shù)組中
(Get an array of all regex matches in a string) result = re.findall(regex, subject)
8.遍歷所有匹配的子串
(Iterate over all matches in a string) for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject) # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()
9.通過(guò)正則表達(dá)式字符串創(chuàng)建一個(gè)正則表達(dá)式對(duì)象
(Create an object to use the same regex for many operations) reobj = re.compile(regex)
10.用法1的正則表達(dá)式對(duì)象版本
(use regex object for if/else branch whether (part of) a string can be matched) reobj = re.compile(regex) if reobj.search(subject): do_something() else: do_anotherthing()
11.用法2的正則表達(dá)式對(duì)象版本
(use regex object for if/else branch whether a string can be matched entirely) reobj = re.compile(r"\Z") #正則表達(dá)式末尾以\Z 結(jié)束 if reobj.match(subject): do_something() else: do_anotherthing()
12.創(chuàng)建一個(gè)正則表達(dá)式對(duì)象,然后通過(guò)該對(duì)象獲得匹配細(xì)節(jié)
(Create an object with details about how the regex object matches (part of) a string) reobj = re.compile(regex) match = reobj.search(subject) if match: # match start: match.start() # match end (exclusive): match.end() # matched text: match.group() do_something() else: do_anotherthing()
13.用正則表達(dá)式對(duì)象獲取匹配子串
(Use regex object to get the part of a string matched by the regex) reobj = re.compile(regex) match = reobj.search(subject) if match: result = match.group() else: result = ""
14.用正則表達(dá)式對(duì)象獲取捕獲組所匹配的子串
(Use regex object to get the part of a string matched by a capturing group) reobj = re.compile(regex) match = reobj.search(subject) if match: result = match.group(1) else: result = ""
15.用正則表達(dá)式對(duì)象獲取有名組所匹配的子串
(Use regex object to get the part of a string matched by a named group)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group("groupname")
else:
result = ""
16.用正則表達(dá)式對(duì)象獲取所有匹配子串并放入數(shù)組
(Use regex object to get an array of all regex matches in a string) reobj = re.compile(regex) result = reobj.findall(subject)
17.通過(guò)正則表達(dá)式對(duì)象遍歷所有匹配子串
(Use regex object to iterate over all matches in a string) reobj = re.compile(regex) for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()
感興趣的讀者可以動(dòng)手調(diào)試一下本文實(shí)例代碼,相信會(huì)有新的收獲。
相關(guān)文章
基于Python實(shí)現(xiàn)最新房?jī)r(jià)信息的獲取
這篇文章主要為大家介紹了如何利用Python獲取房?jī)r(jià)信息(以北京為例),整個(gè)數(shù)據(jù)獲取的信息是通過(guò)房源平臺(tái)獲取的,通過(guò)下載網(wǎng)頁(yè)元素并進(jìn)行數(shù)據(jù)提取分析完成整個(gè)過(guò)程,需要的可以參考一下2022-04-04
在Python程序中實(shí)現(xiàn)分布式進(jìn)程的教程
這篇文章主要介紹了在Python程序中實(shí)現(xiàn)分布式進(jìn)程的教程,在多進(jìn)程編程中十分有用,示例代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
python中ImageTk.PhotoImage()不顯示圖片卻不報(bào)錯(cuò)問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于在python中ImageTk.PhotoImage()不顯示圖片卻不報(bào)錯(cuò)問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
一文帶你精通Python中exec函數(shù)的高級(jí)技巧
在?Python?中,exec?是一個(gè)內(nèi)置函數(shù),允許在運(yùn)行時(shí)動(dòng)態(tài)執(zhí)行?Python?代碼,本文將詳細(xì)介紹?Python?exec?函數(shù)的高級(jí)用法,包括動(dòng)態(tài)代碼生成、執(zhí)行外部文件等內(nèi)容,希望對(duì)大家有所幫助2023-11-11
eclipse創(chuàng)建python項(xiàng)目步驟詳解
在本篇內(nèi)容里小編給大家分享了關(guān)于eclipse創(chuàng)建python項(xiàng)目的具體步驟和方法,需要的朋友們跟著學(xué)習(xí)下。2019-05-05
python 實(shí)現(xiàn)交換兩個(gè)列表元素的位置示例
今天小編就為大家分享一篇python 實(shí)現(xiàn)交換兩個(gè)列表元素的位置示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
在Python中操作文件之truncate()方法的使用教程
這篇文章主要介紹了在Python中操作文件之truncate()方法的使用教程,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
Python操作SQLite數(shù)據(jù)庫(kù)的方法詳解
這篇文章主要介紹了Python操作SQLite數(shù)據(jù)庫(kù)的方法,較為詳細(xì)的分析了Python安裝sqlite數(shù)據(jù)庫(kù)模塊及針對(duì)sqlite數(shù)據(jù)庫(kù)的常用操作技巧,需要的朋友可以參考下2017-06-06

