日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python中的正則表達(dá)式

正則表達(dá)式這個術(shù)語通常被簡稱為正則表達(dá)式。正則表達(dá)式是定義搜索模式的字符序列,主要用于在搜索引擎和文本處理器中執(zhí)行查找和替換操作。

成都創(chuàng)新互聯(lián)公司為客戶提供專業(yè)的成都網(wǎng)站制作、成都做網(wǎng)站、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、成都手機(jī)網(wǎng)站制作等網(wǎng)站方面業(yè)務(wù)。

Python 通過作為標(biāo)準(zhǔn)庫的一部分捆綁的re模塊提供正則表達(dá)式功能。

原始字符串

Python re 模塊中的不同函數(shù)使用原始字符串作為參數(shù)。當(dāng)前綴為“R”或“R”時,普通的字符串成為原始字符串。

Example: Raw String

>>> rawstr = r'Hello! How are you?'
>>> print(rawstr)
Hello! How are you? 

正常字符串和原始字符串的區(qū)別在于 print() 函數(shù)中的正常字符串翻譯轉(zhuǎn)義字符(如\n、\t等)。)的值,而原始字符串中的值則不是。

Example: String vs Raw String

str1 = "Hello!\nHow are you?"
print("normal string:", str1)
str2 = r"Hello!\nHow are you?"
print("raw string:",str2) 

Output

normal string: Hello!
How are you?
raw string: Hello!\nHow are you? 

在上面的例子中,str1(正常字符串)中的\n被翻譯為下一行中正在打印的換行符。 但是,它在str2中被印成了\n——一根生弦。

元字符

當(dāng)某些字符作為部分模式匹配字符串出現(xiàn)時,它們具有特殊的含義。在 Windows 或 Linux DOS 命令中,我們使用*和?-它們類似于元角色。Python 的 re 模塊使用以下字符作為元字符:

*。^ $ + ?[ ] \ | ( )**

當(dāng)一組字母數(shù)字字符放在方括號[]內(nèi)時,目標(biāo)字符串與這些字符匹配。 方括號中可以列出一系列字符或單個字符。例如:

模式 描述
[abc] 匹配任意字符 a、b 或 c
[a-c] 它使用一個范圍來表示同一組字符。
[a-z] 僅匹配小寫字母。
[0-9] 只匹配數(shù)字。

下列特定的字符帶有特定的含義。

模式 描述
\d 匹配任何十進(jìn)制數(shù)字;這相當(dāng)于類[0-9]。
\D 匹配任何非數(shù)字字符
\s 匹配任何空白字符
\S 匹配任何非空白字符
\w 匹配任何字母數(shù)字字符
\W 匹配任何非字母數(shù)字字符。
。 匹配除換行符“\n”以外的任何單個字符。
? 將模式的 0 或 1 匹配到它的左邊
+ 在其左側(cè)出現(xiàn)一個或多個圖案
* 該模式在其左側(cè)出現(xiàn) 0 次或更多次
\b 詞與非詞的界限。/b 與/B 相反
[..] 匹配方括號中的任何單個字符
\ 它用于特殊含義的字符,如。匹配加號的句點(diǎn)或+。
{n,m} 匹配前面的至少 n 次和最多 m 次出現(xiàn)
a| b 匹配 a 或 b

re.match()函數(shù)

re模塊中的這個函數(shù)試圖找出指定的模式是否出現(xiàn)在給定字符串的開頭。

re.match(pattern, string)

如果給定的模式不在開頭,則函數(shù)返回?zé)o,如果找到匹配的對象,則返回匹配的對象。

Example: re.match()

from re import match

mystr = "Welcome to TutorialsTeacher"
obj1 = match("We", mystr)
print(obj1)
obj2 = match("teacher", mystr)
print(obj2) 

Output


None 

匹配對象具有startend屬性。

Example:

>>> print("start:", obj.start(), "end:", obj.end()) 

Output

start: 0 end: 2 

下面的示例演示了如何使用字符范圍來確定一個字符串是否以“W”開頭,后跟一個字母。

Example: match()

from re import match

strings=["Welcome to TutorialsTeacher", "weather forecast","Winston Churchill", "W.G.Grace","Wonders of India", "Water park"]

for string in strings:
    obj = match("W[a-z]",string)
    print(obj) 

Output


None

None

 

re.search()函數(shù)

re.search()函數(shù)在給定字符串的任意位置搜索指定的模式,并在第一次出現(xiàn)時停止搜索。

Example: re.search()

from re import search

string = "Try to earn while you learn"

obj = search("earn", string)
print(obj)
print(obj.start(), obj.end(), obj.group())
7 11 earn 

Output

 

該函數(shù)還返回具有開始和結(jié)束屬性的Match對象。它還給出了一組字符,該模式是其中的一部分。

re.findall()函數(shù)

search()功能相反,findall()繼續(xù)搜索模式,直到目標(biāo)字符串用盡。對象返回所有匹配項的列表。

Example: re.findall()

from re import findall

string = "Try to earn while you learn"

obj = findall("earn", string)
print(obj) 

Output

['earn', 'earn'] 

這個函數(shù)可以用來獲取一個句子中的單詞列表。為此,我們將使用\W*模式。我們還會檢查哪些單詞沒有元音。

Example: re.findall()

obj = findall(r"\w*", "Fly in the sky.")
print(obj)

for word in obj:
    obj= search(r"[aeiou]",word)
    if word!='' and obj==None:
        print(word) 

Output

['Fly', '', 'in', '', 'the', '', 'sky', '', '']
Fly
sky 

re.finditer()函數(shù)

re.finditer()函數(shù)返回目標(biāo)字符串中所有匹配項的迭代器對象。對于每個匹配的組,可以通過 span()屬性獲得開始和結(jié)束位置。

Example: re.finditer()

from re import finditer

string = "Try to earn while you learn"
it = finditer("earn", string)
for match in it:
    print(match.span()) 

Output

(7, 11)
(23, 27) 

re.split()函數(shù)

re.split()功能的工作原理類似于 Python 中str對象的 split() 方法。 每次發(fā)現(xiàn)空白時,它都會拆分給定的字符串。在上面的findall()獲取所有單詞的例子中,列表還包含作為單詞的每個空格。 被re模塊中的split()功能取消。

Example: re.split()

from re import split

string = "Flat is better than nested. Sparse is better than dense."
words = split(r' ', string)
print(words) 

Output

['Flat', 'is', 'better', 'than', 'nested.', 'Sparse', 'is', 'better', 'than', 'dense.'] 

重新編譯()函數(shù)

re.compile()函數(shù)返回一個模式對象,可以在不同的正則表達(dá)式函數(shù)中重復(fù)使用。在下面的例子中,一個字符串“is”被編譯以獲得一個模式對象,并接受search()方法。

Example: re.compile()

from re import *

pattern = compile(r'[aeiou]')
string = "Flat is better than nested. Sparse is better than dense."
words = split(r' ', string) 
for word in words:
    print(word, pattern.match(word)) 

Output

Flat None
is 
better None
than None
nested. None
Sparse None
is 
better None
than None
dense. None 

相同的模式對象可以在搜索帶有元音的單詞時重復(fù)使用,如下所示。

Example: search()

for word in words:
    print(word, pattern.search(word)) 

Output

Flat 
is 
better 
than 
nested. 
Sparse 
is 
better 
than 
dense.  

網(wǎng)站題目:Python中的正則表達(dá)式
分享鏈接:http://m.5511xx.com/article/dhicipp.html