怎么在python中利用re.match()對(duì)多個(gè)字符進(jìn)行匹配-創(chuàng)新互聯(lián)

這篇文章給大家介紹怎么在python中利用re.match()對(duì)多個(gè)字符進(jìn)行匹配,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了婁底免費(fèi)建站歡迎大家使用!

1.  *表示匹配任意多個(gè)字符   \d*表示匹配任意多個(gè)數(shù)字字符

import re
 
text = "123h2ello world"
text1 = "123Hello world456"
text2 = "hello world"
 
res = re.match("\d*", text)  
res1 = re.match("\d*", text1)
res2 = re.match("\d*", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

輸出結(jié)果為

123
123

Process finished with exit code 0

示例2:*

需求:匹配出,一個(gè)字符串第一個(gè)字母為大小字符,后面都是小寫字母并且這些小寫字母可有可無(wú)

import re
#注意是前一個(gè)字符
ret = re.match("[A-Z][a-z]*","M")
print(ret.group())

ret = re.match("[A-Z][a-z]*","AaBcDE")
print(ret.group())

ret = re.match("[A-Z][a-z]*","Aabcdef")
print(ret.group())

#運(yùn)行結(jié)果
M
Aa
Aabcdef

2.  +表示匹配1個(gè)或者多個(gè)任意字符   \w+表示匹配一個(gè)或多個(gè)字母,數(shù)字或下劃線

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
 
res = re.match("\w+", text)
res1 = re.match("\w+", text1)
res2 = re.match("\w+", text2)
 
print(res.group())
print(res1.group())
print(res2)

輸出結(jié)果為

he
Hello
None

Process finished with exit code 0

示例2:+


需求:匹配出,變量名是否有效

import re
names = ["name1","_name","2_name","__name__"]
for i in names:
  ret = re.match("[a-zA-Z_]+[\w]*",i)
  if ret:
    print("變量名 %s 符合要求" % ret.group())
  else:
    print("變量名 %s 非法" % i)

#運(yùn)行結(jié)果
變量名 name1 符合要求
變量名 _name 符合要求
變量名 2_name 非法
變量名 __name__ 符合要求

3.  ?表示匹配0個(gè)或一個(gè)字符     \w?表示匹配0或1個(gè)字母,數(shù)字或下劃線

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
 
res = re.match("\w?", text)
res1 = re.match("\w?", text1)
res2 = re.match("\w?", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

輸出結(jié)果為

h
H

Process finished with exit code 0

示例2:?

需求:匹配出0到99之間的數(shù)字

import re

ret = re.match("[1-9]?[0-9]","7")
print(ret.group())

ret = re.match("[1-9]?\d","33")
print(ret.group())

ret = re.match("[1-9]?\d","09")
print(ret.group())

#運(yùn)行結(jié)果
7
33
0 # 這個(gè)結(jié)果并不是想要的,利用$才能解決

4.  {m}表示匹配m個(gè)字符    \d{11}表示匹配11個(gè)數(shù)字字符

import re
 
text = "he+llo world"
text1 = "Hello world456"
text2 = "hello world"
 
res = re.match("\w{2}", text)
res1 = re.match("\w{3}", text1)
res2 = re.match("\w{4}", text2)
 
print(res.group())
print(res1.group())
print(res2.group())

輸出結(jié)果為

he
Hel
hell

Process finished with exit code 0

示例2:{m}
需求:匹配出,8到20位的密碼,可以是大小寫英文字母、數(shù)字、下劃線

import re
ret = re.match("[a-zA-Z0-9_]{6}","123a3g45678")
print(ret.group())

ret = re.match("[[a-zA-Z0-9_]{8,20}","1ad3123456addfcasdef")
print(ret.group())

#運(yùn)行結(jié)果
123a3g
1ad3123456addfcasdef

5.   {m,n}表示匹配m-n個(gè)字符   \w{2,4}表示匹配2-4個(gè)字符

import re
 
text = "hello world"
text1 = "Helloworld456"
text2 = "hello world"
 
res = re.match("\w{2,5}", text)
res1 = re.match("\w{6,8}", text1)
res2 = re.match("\w{20,25}", text2)
 
print(res.group())
print(res1.group())
print(res2)

hello
Hellowor
None

Process finished with exit code 0


關(guān)于怎么在python中利用re.match()對(duì)多個(gè)字符進(jìn)行匹配就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

網(wǎng)頁(yè)標(biāo)題:怎么在python中利用re.match()對(duì)多個(gè)字符進(jìn)行匹配-創(chuàng)新互聯(lián)
鏈接分享:http://bm7419.com/article42/diohhc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)標(biāo)簽優(yōu)化、網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站