最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python正則表達(dá)式re.match()匹配多個字符方法的實(shí)現(xiàn)

 更新時間:2021年01月27日 09:57:36   作者:zjLOVEcyj  
這篇文章主要介紹了python正則表達(dá)式re.match()匹配多個字符方法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

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

輸出結(jié)果為

123
123

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

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

2.  +表示匹配1個或者多個任意字符   \w+表示匹配一個或多個字母,數(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個或一個字符     \w?表示匹配0或1個字母,數(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 # 這個結(jié)果并不是想要的,利用$才能解決

4.  {m}表示匹配m個字符    \d{11}表示匹配11個數(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個字符   \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)

hello
Hellowor
None

Process finished with exit code 0

到此這篇關(guān)于python正則表達(dá)式re.match()匹配多個字符方法的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python re.match()匹配字符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python多線程semaphore實(shí)現(xiàn)線程數(shù)控制的示例

    python多線程semaphore實(shí)現(xiàn)線程數(shù)控制的示例

    這篇文章主要介紹了python多線程semaphore實(shí)現(xiàn)線程數(shù)控制的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Python對excel的基本操作方法

    Python對excel的基本操作方法

    這篇文章主要介紹了Python對excel的基本操作,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Python基于numpy模塊實(shí)現(xiàn)回歸預(yù)測

    Python基于numpy模塊實(shí)現(xiàn)回歸預(yù)測

    這篇文章主要介紹了Python基于numpy模塊實(shí)現(xiàn)回歸預(yù)測,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • Python中with及contextlib的用法詳解

    Python中with及contextlib的用法詳解

    這篇文章主要介紹了Python中with及contextlib的用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了with及contextlib的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-06-06
  • 使用Python3編寫抓取網(wǎng)頁和只抓網(wǎng)頁圖片的腳本

    使用Python3編寫抓取網(wǎng)頁和只抓網(wǎng)頁圖片的腳本

    這篇文章主要介紹了使用Python3編寫抓取網(wǎng)頁和只抓網(wǎng)頁圖片的腳本,使用到了urllib模塊,需要的朋友可以參考下
    2015-08-08
  • opencv中cv2.minAreaRect函數(shù)輸出角度問題詳解

    opencv中cv2.minAreaRect函數(shù)輸出角度問題詳解

    minAreaRect返回的數(shù)據(jù)包括了矩形的中心點(diǎn),寬、高,和旋轉(zhuǎn)角度,下面這篇文章主要給大家介紹了關(guān)于opencv中cv2.minAreaRect函數(shù)輸出角度問題的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • python簡單的函數(shù)定義和用法實(shí)例

    python簡單的函數(shù)定義和用法實(shí)例

    這篇文章主要介紹了python簡單的函數(shù)定義和用法,實(shí)例分析了Python自定義函數(shù)及其使用方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-05-05
  • python 讀寫csv文件方式(創(chuàng)建,追加,覆蓋)

    python 讀寫csv文件方式(創(chuàng)建,追加,覆蓋)

    這篇文章主要介紹了python 讀寫csv文件方式(創(chuàng)建,追加,覆蓋),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python大數(shù)據(jù)量文本文件高效解析方案代碼實(shí)現(xiàn)全過程

    Python大數(shù)據(jù)量文本文件高效解析方案代碼實(shí)現(xiàn)全過程

    在數(shù)據(jù)分析中,有時數(shù)據(jù)源會是超大的文本文件(幾G,或在幾十G),需要從中提取需要的信息,下面這篇文章主要給大家介紹了關(guān)于Python大數(shù)據(jù)量文本文件高效解析方案代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Python 制作詞云的WordCloud參數(shù)用法說明

    Python 制作詞云的WordCloud參數(shù)用法說明

    這篇文章主要介紹了Python 制作詞云的WordCloud參數(shù)用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論

铜陵市| 祁东县| 佛学| 乳山市| 三亚市| 兴山县| 通山县| 永安市| 河津市| 宁晋县| 出国| 清水河县| 浮梁县| 朝阳市| 都兰县| 水富县| 开江县| 嘉兴市| 黄大仙区| 当雄县| 南充市| 枣庄市| 图木舒克市| 长丰县| 沁源县| 广水市| 玉门市| 乐平市| 稻城县| 吕梁市| 江安县| 库伦旗| 嘉祥县| 满洲里市| 通城县| 刚察县| 阳谷县| 南木林县| 临沭县| 巴中市| 宜兰市|