国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

python正則表達式re.match()匹配多個字符方法的實現

瀏覽:89日期:2022-06-29 08:49:17
1. *表示匹配任意多個字符 d*表示匹配任意多個數字字符

import re text = '123h1ello 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())

輸出結果為

123123

Process finished with exit code 0

示例2:*

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

import re#注意是前一個字符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())

#運行結果MAaAabcdef

2. +表示匹配1個或者多個任意字符 w+表示匹配一個或多個字母,數字或下劃線

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)

輸出結果為

heHelloNone

Process finished with exit code 0

示例2:+

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

import renames = ['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)

#運行結果變量名 name1 符合要求變量名 _name 符合要求變量名 2_name 非法變量名 __name__ 符合要求

3. ?表示匹配0個或一個字符 w?表示匹配0或1個字母,數字或下劃線

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())

輸出結果為

hH

Process finished with exit code 0

示例2:?

需求:匹配出0到99之間的數字

import reret = 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())

#運行結果7330 # 這個結果并不是想要的,利用$才能解決

4. {m}表示匹配m個字符 d{11}表示匹配11個數字字符

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())

輸出結果為

heHelhell

Process finished with exit code 0

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

import reret = re.match('[a-zA-Z0-9_]{6}','123a3g45678')print(ret.group())ret = re.match('[[a-zA-Z0-9_]{8,20}','1ad3123456addfcasdef')print(ret.group())

#運行結果123a3g1ad3123456addfcasdef

5. {m,n}表示匹配m-n個字符 w{2,4}表示匹配2-4個字符

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)

helloHelloworNone

Process finished with exit code 0

到此這篇關于python正則表達式re.match()匹配多個字符方法的實現的文章就介紹到這了,更多相關python re.match()匹配字符內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 成人网18免费软件 | 欧美午夜免费一级毛片 | 国产精品系列在线一区 | 202z欧美成人 | 免费91最新地址永久入口 | 欧美性色生活片免费播放 | 国产成人盗拍精品免费视频 | 国产一区二区精品久久凹凸 | 欧美白人和黑人xxxx猛交视频 | 免费亚洲视频 | 九九亚洲 | 成人小视频在线播放 | 亚洲一区成人 | 九九视频在线观看 | 久草免费资源站 | 操美国女人| 五月久久噜噜噜色影 | 久久91精品国产一区二区 | 一级做a爱过程免费视 | 91网站国产| 另类专区另类专区亚洲 | 特级a欧美孕妇做爰片毛片 特级a欧美做爰片毛片 | 成人黄色免费看 | 亚洲精品综合一区在线 | 日本久草网| 国产成人教育视频在线观看 | 香蕉97碰碰视频免费 | 久久国内精品自在自线400部o | 91精品久久久久久久久久 | 3至13呦女毛片 | 毛片手机在线视频免费观看 | 免费一级特黄 欧美大片 | 日韩久久影院 | avtom影院入口永久在线 | 精品国产品国语在线不卡丶 | 国产精品专区第二 | 国产l精品国产亚洲区久久 国产tv在线 | 久久精品亚洲 | 久久福利青草精品资源 | 免费一级毛片在线观看 | 久久久精品免费观看 |