您好,登錄后才能下訂單哦!
正則表達式
正則表達式是一種強大的字符串操作工具。它是一種領(lǐng)域特定語言 (DSL),不管是 Python 還是在大多數(shù)現(xiàn)代編程語言中都是作為庫存在。
它們主要面向兩種任務(wù):
- 驗證字符串是否與模式匹配 (例如,字符串具有電子郵件地址的格式)。
- 在字符串中執(zhí)行替換(例如將所有大寫字母改成小寫字母)。
特定于領(lǐng)域的語言是高度專業(yè)化的迷你編程語言。
正則表達式是一個例子,SQL(用于數(shù)據(jù)庫操作)是另一個例子。
私有領(lǐng)域特定語言通常用于特定的工業(yè)目的。
Python 的正則表達式可以使用 re 模塊訪問,re 模塊是標準庫的一部分。
當(dāng)你定義一個正則表達式,可以使用 re.match 函數(shù)用于確定是否匹配字符串的開始部分。如果匹配則 match 函數(shù)返回表示匹配的對象,如果不匹配則返回 None。
為了避免在處理正則表達式時出現(xiàn)混淆,我們將 r 添加到字符串前綴。該字符串不需要轉(zhuǎn)義任何東西,使得正則表達式的使用變得更容易。
from re import match msg = r"super" if match(msg,"superman!"): print("You are True") else: print("Occur an error! Foolish...")
運行結(jié)果:
>>>
You are True
>>>
上面的例子檢查模式 super 是否匹配字符串,如果匹配,則打印 You are True。
這里的模式是一種簡單的單詞,但是有些字符串,在正則表達式中使用它們時會有特殊的意義。
匹配模式的其他函數(shù)有 re.match
和 re.findall
。
re.match 在字符串中找到匹配。
re.findall 返回一個包含匹配的列表。
import re string = "Hello python!Hello python!Hello python!" pattern = r".python." print(re.match(pattern,string)) print(re.findall(pattern,string))
運行結(jié)果:
>>>
None
[' python!', ' python!', ' python!']
>>>
從上面的示例中,我們可以得出:
match() 函數(shù)是從內(nèi)容的第一個字符開始匹配,如果匹配不到,就得到None
findall() 函數(shù)從全部內(nèi)容匹配,如果有多個,找出所有匹配的
函數(shù) re.finditer 執(zhí)行與 re.findall 相同的操作,但它返回一個迭代器,而不是一個列表。
正則表達式的 search 函數(shù)返回一個對象,包含幾個更詳細的信息。
此方法包括返回字符串匹配的值,返回第一次匹配的開始和結(jié)束位置,以及以元組形式返回第一個匹配的開始和結(jié)束位置的 span 函數(shù)。
import re string = "Hello python!Hello python!Hello python!" pattern = r".python." match = re.search(pattern,string) if match: print(match.group()) print(match.start()) print(match.end()) print(match.span())
運行結(jié)果:
>>>
python!
5
13
(5, 13)
>>>
查找和替換
sub 是正則表達式里非常重要的函數(shù)。表達式:
re.sub(pattern, repl, string, count=0, flags=0)
pattern:表示正則表達式中的模式字符串;
repl:被替換的字符串(既可以是字符串,也可以是函數(shù));
string:要被處理的,要被替換的字符串;
count:匹配的次數(shù), 默認是全部替換
flags:具體用處不詳
import re string = "Hello python!Hello python!Hello python!" pattern = r"python" newstr = re.sub(pattern,"Java",string) print(newstr)
運行結(jié)果:
>>>
Hello Java!Hello Java!Hello Java!
>>>
元字符
元字符使正則表達式比普通字符串方法更強大。它們允許您創(chuàng)建正則表達式來表示諸如一個或多個數(shù)字的匹配。
如果要創(chuàng)建與元字符 (如 $) 匹配的正則表達式,元字符的存在就會產(chǎn)生問題。您可以通過在元字符前面添加反斜杠來轉(zhuǎn)義元字符。
但是這可能會導(dǎo)致問題,因為反斜杠在普通 Python 字符串中也有轉(zhuǎn)義函數(shù)。這可能意味著可能將三個或四個反斜杠排成一行來執(zhí)行所有轉(zhuǎn)義操作。
為了避免這種情況,您可以使用一個原始字符串,它是一個普通字符串,前面有一個 "r" 前綴。
元字符點,用來表示匹配除了換行外的任何字符。
import re string1 = "Hello python!Hello python!Hello python!" string2 = "pythan,1234587pythoi" string3 = r"hello" pattern = r"pyth.n" match2 = re.search(pattern,string1) match3 = re.search(pattern,string2) match4 = re.search(pattern,string3) if match2: print(match2.group()) print("match 1") if match3: print(match2.group()) print("match 2") if match4: print(match4.group()) print("match 3")
運行結(jié)果:
>>>
python
match 1
python
match 2
>>>
^ 表示匹配開始,$ 表示匹配結(jié)束。
import re string1="python" string2="pythan,1234587pythoi" string3="hello" pattern=r"^pyth.n$" match2 = re.search(pattern,string1) match3 = re.search(pattern,string2) match4 = re.search(pattern,string3) if match2: print(match2.group()) print("match 1") if match3: print(match2.group()) print("match 2") if match4: print(match4.group()) print("match 3")
運行結(jié)果:
>>>
python
match 1
>>>
匹配模式 "^pyth.n$"
意味著字符串應(yīng)該以 pyth 開頭,然后是一個除換行符以外的任何字符,并以 n 結(jié)尾。
總結(jié)
以上所述是小編給大家介紹的Python正則表達式和元字符,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。