Python入門(mén)教程(二十九)Python的RegEx正則表達(dá)式
RegEx 或正則表達(dá)式是形成搜索模式的字符序列。
RegEx 可用于檢查字符串是否包含指定的搜索模式。
RegEx 模塊
Python 提供名為 re 的內(nèi)置包,可用于處理正則表達(dá)式。
導(dǎo)入 re 模塊:
import re
Python 中的 RegEx
導(dǎo)入 re 模塊后,就可以開(kāi)始使用正則表達(dá)式了:
實(shí)例
檢索字符串以查看它是否以 “China” 開(kāi)頭并以 “country” 結(jié)尾:
import re txt = "China is a great country" x = re.search("^China.*country$", txt)
運(yùn)行實(shí)例
import re txt = "China is a great country" x = re.search("^China.*country$", txt) if (x): print("YES! We have a match!") else: print("No match")
RegEx 函數(shù)
re 模塊提供了一組函數(shù),允許我們檢索字符串以進(jìn)行匹配:
元字符
元字符是具有特殊含義的字符
字符:[] 描述:一組字符 示例:“[a-m]”
import re str = "The rain in Spain" #Find all lower case characters alphabetically between "a" and "m": x = re.findall("[a-m]", str) print(x)
運(yùn)行示例
字符: 描述:示意特殊序列(也可用于轉(zhuǎn)義特殊字符) 示例:“\d”
import re str = "That will be 59 dollars" #Find all digit characters: x = re.findall("\d", str) print(x)
運(yùn)行示例
字符:. 描述:任何字符(換行符除外) 示例: “he…o”
import re str = "hello world" #Search for a sequence that starts with "he", followed by two (any) characters, and an "o": x = re.findall("he..o", str) print(x)
運(yùn)行示例
字符:^ 描述:起始于 示例: “^hello”
import re str = "hello world" #Check if the string starts with 'hello': x = re.findall("^hello", str) if (x): print("Yes, the string starts with 'hello'") else: print("No match")
運(yùn)行示例
字符:$ 描述:結(jié)束于 示例:“world$”
import re str = "hello world" #Check if the string ends with 'world': x = re.findall("world$", str) if (x): print("Yes, the string ends with 'world'") else: print("No match")
運(yùn)行示例
字符:* 描述:零次或多次出現(xiàn) 示例:“aix*”
import re str = "The rain in Spain falls mainly in the plain!" #Check if the string contains "ai" followed by 0 or more "x" characters: x = re.findall("aix*", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:+ 描述:一次或多次出現(xiàn) 示例: “aix+”
import re str = "The rain in Spain falls mainly in the plain!" #Check if the string contains "ai" followed by 1 or more "x" characters: x = re.findall("aix+", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:{} 描述: 確切地指定的出現(xiàn)次數(shù) 示例:“al{2}”
import re str = "The rain in Spain falls mainly in the plain!" #Check if the string contains "a" followed by exactly two "l" characters: x = re.findall("al{2}", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:| 描述:兩者任一 示例:“falls|stays”
import re str = "The rain in Spain falls mainly in the plain!" #Check if the string contains either "falls" or "stays": x = re.findall("falls|stays", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:() 描述:捕獲和分組
特殊序列
特殊序列指的是 \ 后跟下表中的某個(gè)字符,擁有特殊含義。
字符:\A 描述:如果指定的字符位于字符串的開(kāi)頭,則返回匹配項(xiàng) 示例:“\AThe”
import re str = "The rain in Spain" #Check if the string starts with "The": x = re.findall("\AThe", str) print(x) if (x): print("Yes, there is a match!") else: print("No match")
運(yùn)行示例
字符:\b
描述:返回指定字符位于單詞的開(kāi)頭或末尾的匹配項(xiàng)
示例:r"\bain"
import re str = "The rain in Spain" #Check if "ain" is present at the beginning of a WORD: x = re.findall(r"\bain", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
示例:r"ain\b"
import re str = "The rain in Spain" #Check if "ain" is present at the end of a WORD: x = re.findall(r"ain\b", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:\B
描述:返回指定字符存在的匹配項(xiàng),但不在單詞的開(kāi)頭(或結(jié)尾處)
示例:r"\Bain"
import re str = "The rain in Spain" #Check if "ain" is present, but NOT at the beginning of a word: x = re.findall(r"\Bain", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
示例:r"ain\B"
import re str = "The rain in Spain" #Check if "ain" is present, but NOT at the end of a word: x = re.findall(r"ain\B", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:\d
描述:返回字符串包含數(shù)字的匹配項(xiàng)(數(shù)字 0-9)
示例:“\d”
import re str = "The rain in Spain" #Check if the string contains any digits (numbers from 0-9): x = re.findall("\d", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:\D
描述:返回字符串不包含數(shù)字的匹配項(xiàng)
示例:“\D”
import re str = "The rain in Spain" #Return a match at every no-digit character: x = re.findall("\D", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:\s
描述:返回字符串包含空白字符的匹配項(xiàng)
示例:“\s”
import re str = "The rain in Spain" #Return a match at every white-space character: x = re.findall("\s", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:\S
描述:返回字符串不包含空白字符的匹配項(xiàng)
示例:“\S”
import re str = "The rain in Spain" #Return a match at every NON white-space character: x = re.findall("\S", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:\w
描述: 返回一個(gè)匹配項(xiàng),其中字符串包含任何單詞字符 (從 a 到 Z 的字符,從 0 到 9 的數(shù)字和下劃線 _ 字符)
示例:“\w”
import re str = "The rain in Spain" #Return a match at every word character (characters from a to Z, digits from 0-9, and the underscore _ character): x = re.findall("\w", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:\W
描述:返回一個(gè)匹配項(xiàng),其中字符串不包含任何單詞字符
示例:“\W”
import re str = "The rain in Spain" #Return a match at every NON word character (characters NOT between a and Z. Like "!", "?" white-space etc.): x = re.findall("\W", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:\Z
描述:如果指定的字符位于字符串的末尾,則返回匹配項(xiàng) 。
示例:“Spain\Z”
import re str = "The rain in Spain" #Check if the string ends with "Spain": x = re.findall("Spain\Z", str) print(x) if (x): print("Yes, there is a match!") else: print("No match")
運(yùn)行示例
集合(Set)
集合(Set)是一對(duì)方括號(hào) [] 內(nèi)的一組字符,具有特殊含義。
字符:[arn]
描述:返回一個(gè)匹配項(xiàng),其中存在指定字符(a,r 或 n)之一
示例
import re str = "The rain in Spain" #Check if the string has any a, r, or n characters: x = re.findall("[arn]", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:[a-n]
描述:返回字母順序 a 和 n 之間的任意小寫(xiě)字符匹配項(xiàng)
示例
import re str = "The rain in Spain" #Check if the string has any characters between a and n: x = re.findall("[a-n]", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:[^arn]
描述:返回除 a、r 和 n 之外的任意字符的匹配項(xiàng)
示例
import re str = "The rain in Spain" #Check if the string has other characters than a, r, or n: x = re.findall("[^arn]", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:[0123]
描述:返回存在任何指定數(shù)字(0、1、2 或 3)的匹配項(xiàng)
示例
import re str = "The rain in Spain" #Check if the string has any 0, 1, 2, or 3 digits: x = re.findall("[0123]", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:[0-9]
描述:返回 0 與 9 之間任意數(shù)字的匹配
示例
import re str = "8 times before 11:45 AM" #Check if the string has any digits: x = re.findall("[0-9]", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:[0-5][0-9]
描述:返回介于 0 到 9 之間的任何數(shù)字的匹配項(xiàng)
示例
import re str = "8 times before 11:45 AM" #Check if the string has any two-digit numbers, from 00 to 59: x = re.findall("[0-5][0-9]", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:[a-zA-Z]
描述:返回字母順序 a 和 z 之間的任何字符的匹配,小寫(xiě)或大寫(xiě)
示例
import re str = "8 times before 11:45 AM" #Check if the string has any characters from a to z lower case, and A to Z upper case: x = re.findall("[a-zA-Z]", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
字符:[+]
描述:在集合中,+、*、.、|、()、$、{} 沒(méi)有特殊含義,因此 [+] 表示:返回字符串中任何 + 字符的匹配項(xiàng)。
示例
import re str = "8 times before 11:45 AM" #Check if the string has any + characters: x = re.findall("[+]", str) print(x) if (x): print("Yes, there is at least one match!") else: print("No match")
運(yùn)行示例
findall() 函數(shù)
findall() 函數(shù)返回包含所有匹配項(xiàng)的列表。
實(shí)例
打印所有匹配的列表
import re str = "China is a great country" x = re.findall("a", str) print(x)
運(yùn)行實(shí)例
這個(gè)列表以被找到的順序包含匹配項(xiàng)。
如果未找到匹配項(xiàng),則返回空列表。
實(shí)例
如果未找到匹配,則返回空列表:
import re str = "China is a great country" x = re.findall("USA", str) print(x)
運(yùn)行實(shí)例
search() 函數(shù)
search() 函數(shù)搜索字符串中的匹配項(xiàng),如果存在匹配則返回 Match 對(duì)象。
如果有多個(gè)匹配,則僅返回首個(gè)匹配項(xiàng)。
實(shí)例
在字符串中搜索第一個(gè)空白字符
import re str = "China is a great country" x = re.search("\s", str) print("The first white-space character is located in position:", x.start())
運(yùn)行實(shí)例
如果未找到匹配,則返回值 None:
實(shí)例
進(jìn)行不返回匹配的檢索
import re str = "China is a great country" x = re.search("USA", str) print(x)
運(yùn)行實(shí)例
split() 函數(shù)
split() 函數(shù)返回一個(gè)列表,其中字符串在每次匹配時(shí)被拆分。
實(shí)例
在每個(gè)空白字符處進(jìn)行拆分
import re str = "China is a great country" x = re.split("\s", str) print(x)
運(yùn)行實(shí)例
可以通過(guò)指定 maxsplit 參數(shù)來(lái)控制出現(xiàn)次數(shù):
實(shí)例
僅在首次出現(xiàn)時(shí)拆分字符串:
import re str = "China is a great country" x = re.split("\s", str, 1) print(x)
運(yùn)行實(shí)例
sub() 函數(shù)
sub() 函數(shù)把匹配替換為您選擇的文本
實(shí)例
用數(shù)字 9 替換每個(gè)空白字符
import re str = "China is a great country" x = re.sub("\s", "9", str) print(x)
運(yùn)行實(shí)例
可以通過(guò)指定 count 參數(shù)來(lái)控制替換次數(shù):
實(shí)例
替換前兩次出現(xiàn)
import re str = "China is a great country" x = re.sub("\s", "9", str, 2) print(x)
運(yùn)行實(shí)例
Match 對(duì)象
Match 對(duì)象是包含有關(guān)搜索和結(jié)果信息的對(duì)象。
注釋?zhuān)喝绻麤](méi)有匹配,則返回值 None,而不是 Match 對(duì)象。
實(shí)例
執(zhí)行會(huì)返回 Match 對(duì)象的搜索:
import re str = "China is a great country" x = re.search("a", str) print(x) # 將打印一個(gè)對(duì)象
運(yùn)行實(shí)例
Match 對(duì)象提供了用于取回有關(guān)搜索及結(jié)果信息的屬性和方法:
span()
返回的元組包含了匹配的開(kāi)始和結(jié)束位置.string
返回傳入函數(shù)的字符串group()
返回匹配的字符串部分
實(shí)例
打印首個(gè)匹配出現(xiàn)的位置(開(kāi)始和結(jié)束位置)。
正則表達(dá)式查找以大寫(xiě) “C” 開(kāi)頭的任何單詞:
import re str = "China is a great country" x = re.search(r"\bC\w+", str) print(x.span())
運(yùn)行實(shí)例
實(shí)例
打印傳入函數(shù)的字符串
import re str = "China is a great country" x = re.search(r"\bC\w+", str) print(x.string)
運(yùn)行實(shí)例
實(shí)例
打印匹配的字符串部分
正則表達(dá)式查找以大寫(xiě) “C” 開(kāi)頭的任何單詞:
import re str = "China is a great country" x = re.search(r"\bC\w+", str) print(x.group())
運(yùn)行實(shí)例
注釋?zhuān)喝绻麤](méi)有匹配項(xiàng),則返回值 None,而不是 Match 對(duì)象。
到此這篇關(guān)于Python入門(mén)教程(二十九)Python的RegEx的文章就介紹到這了,更多相關(guān)Python的RegEx內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python tornado隊(duì)列示例-一個(gè)并發(fā)web爬蟲(chóng)代碼分享
這篇文章主要介紹了Python tornado隊(duì)列示例-一個(gè)并發(fā)web爬蟲(chóng)代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01python神經(jīng)網(wǎng)絡(luò)Inception?ResnetV2模型復(fù)現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Inception?ResnetV2模型復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05聊聊基于pytorch實(shí)現(xiàn)Resnet對(duì)本地?cái)?shù)據(jù)集的訓(xùn)練問(wèn)題
本文項(xiàng)目是使用Resnet模型來(lái)識(shí)別螞蟻和蜜蜂,其一共有三百九十六張的數(shù)據(jù),訓(xùn)練集只有兩百多張(數(shù)據(jù)集很小),運(yùn)行十輪后,分別對(duì)訓(xùn)練集和測(cè)試集在每一輪的準(zhǔn)確率,對(duì)pytorch實(shí)現(xiàn)Resnet本地?cái)?shù)據(jù)集的訓(xùn)練感興趣的朋友一起看看吧2022-03-03Pycharm遠(yuǎn)程連接服務(wù)器并運(yùn)行與調(diào)試
本篇文章介紹一下 Pycharm 如何配置遠(yuǎn)程連接信息,使其能夠在本地使用服務(wù)器上的GPU等硬件資源,并在本地完成代碼的運(yùn)行與調(diào)試,感興趣的可以了解一下2021-08-08Django項(xiàng)目創(chuàng)建到啟動(dòng)詳解(最全最詳細(xì))
這篇文章主要給大家介紹了關(guān)于Django項(xiàng)目創(chuàng)建到啟動(dòng)的步驟,本文介紹的方法算是最全最詳細(xì)的一個(gè)項(xiàng)目,需要的朋友可以參考下2019-09-09python 如何將數(shù)據(jù)寫(xiě)入本地txt文本文件的實(shí)現(xiàn)方法
這篇文章主要介紹了python 如何將數(shù)據(jù)寫(xiě)入本地txt文本文件的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09