亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python正則表達(dá)式和元字符詳解

 更新時(shí)間:2018年11月29日 08:33:09   作者:清潔工老板  
這篇文章主要介紹了Python正則表達(dá)式和元字符詳解,需要的朋友可以參考下

正則表達(dá)式

正則表達(dá)式是一種強(qiáng)大的字符串操作工具。它是一種領(lǐng)域特定語言 (DSL),不管是 Python 還是在大多數(shù)現(xiàn)代編程語言中都是作為庫存在。

它們主要面向兩種任務(wù):

- 驗(yàn)證字符串是否與模式匹配 (例如,字符串具有電子郵件地址的格式)。
- 在字符串中執(zhí)行替換(例如將所有大寫字母改成小寫字母)。

特定于領(lǐng)域的語言是高度專業(yè)化的迷你編程語言。

正則表達(dá)式是一個(gè)例子,SQL(用于數(shù)據(jù)庫操作)是另一個(gè)例子。

私有領(lǐng)域特定語言通常用于特定的工業(yè)目的。

Python 的正則表達(dá)式可以使用 re 模塊訪問,re 模塊是標(biāo)準(zhǔn)庫的一部分。

當(dāng)你定義一個(gè)正則表達(dá)式,可以使用 re.match 函數(shù)用于確定是否匹配字符串的開始部分。如果匹配則 match 函數(shù)返回表示匹配的對(duì)象,如果不匹配則返回 None。

為了避免在處理正則表達(dá)式時(shí)出現(xiàn)混淆,我們將 r 添加到字符串前綴。該字符串不需要轉(zhuǎn)義任何東西,使得正則表達(dá)式的使用變得更容易。

from re import match
msg = r"super"
if match(msg,"superman!"):
 print("You are True")
else:
 print("Occur an error! Foolish...")

運(yùn)行結(jié)果:

>>>
You are True
>>>

上面的例子檢查模式 super 是否匹配字符串,如果匹配,則打印 You are True。

這里的模式是一種簡單的單詞,但是有些字符串,在正則表達(dá)式中使用它們時(shí)會(huì)有特殊的意義。

匹配模式的其他函數(shù)有 re.match re.findall。

re.match 在字符串中找到匹配。
re.findall 返回一個(gè)包含匹配的列表。

import re
string = "Hello python!Hello python!Hello python!"
pattern = r".python."
print(re.match(pattern,string))
print(re.findall(pattern,string))

運(yùn)行結(jié)果:

>>>
None
[' python!', ' python!', ' python!']
>>>

從上面的示例中,我們可以得出:

match() 函數(shù)是從內(nèi)容的第一個(gè)字符開始匹配,如果匹配不到,就得到None
findall() 函數(shù)從全部內(nèi)容匹配,如果有多個(gè),找出所有匹配的

函數(shù) re.finditer 執(zhí)行與 re.findall 相同的操作,但它返回一個(gè)迭代器,而不是一個(gè)列表。

正則表達(dá)式的 search 函數(shù)返回一個(gè)對(duì)象,包含幾個(gè)更詳細(xì)的信息。

此方法包括返回字符串匹配的值,返回第一次匹配的開始和結(jié)束位置,以及以元組形式返回第一個(gè)匹配的開始和結(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())

運(yùn)行結(jié)果:

>>>
python!
5
13
(5, 13)
>>>

查找和替換

sub 是正則表達(dá)式里非常重要的函數(shù)。表達(dá)式:

re.sub(pattern, repl, string, count=0, flags=0)

pattern:表示正則表達(dá)式中的模式字符串;
repl:被替換的字符串(既可以是字符串,也可以是函數(shù));
string:要被處理的,要被替換的字符串;
count:匹配的次數(shù), 默認(rèn)是全部替換
flags:具體用處不詳

import re
string = "Hello python!Hello python!Hello python!"
pattern = r"python"
newstr = re.sub(pattern,"Java",string)
print(newstr)

運(yùn)行結(jié)果:

>>>
Hello Java!Hello Java!Hello Java!
>>>

元字符

元字符使正則表達(dá)式比普通字符串方法更強(qiáng)大。它們?cè)试S您創(chuàng)建正則表達(dá)式來表示諸如一個(gè)或多個(gè)數(shù)字的匹配。
如果要?jiǎng)?chuàng)建與元字符 (如 $) 匹配的正則表達(dá)式,元字符的存在就會(huì)產(chǎn)生問題。您可以通過在元字符前面添加反斜杠來轉(zhuǎn)義元字符。

但是這可能會(huì)導(dǎo)致問題,因?yàn)榉葱备茉谄胀?Python 字符串中也有轉(zhuǎn)義函數(shù)。這可能意味著可能將三個(gè)或四個(gè)反斜杠排成一行來執(zhí)行所有轉(zhuǎn)義操作。

為了避免這種情況,您可以使用一個(gè)原始字符串,它是一個(gè)普通字符串,前面有一個(gè) "r" 前綴。

元字符點(diǎn),用來表示匹配除了換行外的任何字符。

import re
string1 = "Hello python!Hello python!Hello python!"
string2 = "pythan,1234587pythoi"
string3 = r"hello"
pattern = r"pyth.n"
match1 = re.search(pattern,string1)
match2 = re.search(pattern,string2)
match3 = re.search(pattern,string3)
if match1:
 print(match1.group())
 print("match 1")
if match2:
 print(match1.group())
 print("match 2")
if match3:
 print(match3.group())
 print("match 3")

運(yùn)行結(jié)果:

>>>
python
match 1
python
match 2
>>>

^ 表示匹配開始,$ 表示匹配結(jié)束。

import re
string1="python"
string2="pythan,1234587pythoi"
string3="hello"
pattern=r"^pyth.n$"
match1 = re.search(pattern,string1)
match2 = re.search(pattern,string2)
match3 = re.search(pattern,string3)
if match1:
 print(match1.group())
 print("match 1")
if match2:
 print(match1.group())
 print("match 2")
if match3:
 print(match3.group())
 print("match 3")

運(yùn)行結(jié)果:

>>>
python
match 1
>>>

匹配模式 "^pyth.n$" 意味著字符串應(yīng)該以 pyth 開頭,然后是一個(gè)除換行符以外的任何字符,并以 n 結(jié)尾。

總結(jié)

以上所述是小編給大家介紹的Python正則表達(dá)式和元字符,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論