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

python正則表達(dá)式最詳解

 更新時間:2021年11月04日 08:51:47   作者:(∪.∪ )...zzz  
篇文章主要介紹了Python中正則表達(dá)式的詳細(xì)解釋,正則表達(dá)式是Python學(xué)習(xí)進(jìn)階當(dāng)中的重要內(nèi)容,需要的朋友可以參考下

一、正則表達(dá)式–元字符

re 模塊使 Python 語言擁有全部的正則表達(dá)式功能

在這里插入圖片描述

1. 數(shù)量詞

# 提取大小寫字母混合的單詞
import re
a = 'Excel 12345Word23456PPT12Lr'
r = re.findall('[a-zA-Z]{3,5}',a)
# 提取字母的數(shù)量3個到5個
print(r)
# ['Excel', 'Word', 'PPT']
# 貪婪 與 非貪婪  【Python默認(rèn)使用貪婪模式】
# 貪婪:'[a-zA-Z]{3,5}'
# 非貪婪:'[a-zA-Z]{3,5}?' 或 '[a-zA-Z]{3}'
# 建議使用后者,不要使用?號,否則你會與下面的?號混淆
# 匹配0次或無限多次 *號,*號前面的字符出現(xiàn)0次或無限次
import re
a = 'exce0excell3excel3'
r = re.findall('excel*',a)
r = re.findall('excel.*',a) # ['excell3excel3']
# excel 沒有l(wèi) 有很多l(xiāng)都可以匹配出來
print(r)
# ['exce', 'excell', 'excel']
# 匹配1次或者無限多次 +號,+號前面的字符至少出現(xiàn)1次
import re
a = 'exce0excell3excel3'
r = re.findall('excel+',a)
print(r)
# ['excell', 'excel']
# 匹配0次或1次  ?號,?號經(jīng)常用來去重復(fù)
import re
a = 'exce0excell3excel3'
r = re.findall('excel?',a)
print(r)
# ['exce', 'excel', 'excel']

2. 字符匹配

在這里插入圖片描述

line = 'xyz,xcz.xfc.xdz,xaz,xez,xec'
r = re.findall('x[de]z', line)
# pattern 是x開始,z結(jié)束,含d或e
print(r)
# ['xdz', 'xez']
r = re.findall('x[^de]z', line)
# pattern 是x開始,z結(jié)束,不是含d或e
print(r)
# ['xyz', 'xcz', 'xaz']
# \w 可以提取中文,英文,數(shù)字和下劃線,不能提取特殊字符
import re
a = 'Excel 12345Word\n23456_PPT12lr'
r = re.findall('\w',a)
print(r)
# ['E', 'x', 'c', 'e', 'l', '1', '2', '3', '4', '5', 'W', 'o', 'r', 'd', '2', '3', '4', '5', '6', '_', 'P', 'P', 'T', '1', '2', 'l', 'r']
# \W 提取特殊字符,空格 \n \t
import re
a = 'Excel 12345Word\n23456_PPT12lr'
r = re.findall('\W',a)
print(r)
# [' ', '\n']

3. 邊界匹配

在這里插入圖片描述

# 限制電話號碼的位置必需是8-11位才能提取
import re
tel = '13811115888'
r = re.findall('^\d{8,11}$',tel)
print(r)
# ['13811115888']

4. 組

# 將abc打成一個組,{2}指的是重復(fù)幾次,匹配abcabc
import re
a = 'abcabcabcxyzabcabcxyzabc'
r = re.findall('(abc){2}',a)  # 與
# ['abc', 'abc']
print(r)
r = re.findall('(abc){3}',a)
# ['abc']

5. 匹配模式參數(shù)

在這里插入圖片描述

# findall第三參數(shù) re.I忽略大小寫
import re
a = 'abcFBIabcCIAabc'
r = re.findall('fbi',a,re.I)
print(r)
# ['FBI']
# 多個模式之間用 | 連接在一起
import re
a = 'abcFBI\nabcCIAabc'
r = re.findall('fbi.{1}',a,re.I | re.S)
# 匹配fbi然后匹配任意一個字符包括\n
print(r)
# ['FBI\n']

二、方法

re.findall

  • 匹配出字符串中所有 與制定值相關(guān)的值
  • 以列表的形式返回
  • 未匹配則返回空列表
import re
re.findall(pattern, string, flags=0)
pattern.findall(string[ , pos[ , endpos]])
import re
line = "111aaabbb222小呼嚕奧利奧"
r = re.findall('[0-9]',line)
print(r)
# ['1', '1', '1', '2', '2', '2']

re.match

  • re.match 嘗試從字符串的起始位置匹配一個模式
  • 如果不是起始位置匹配成功的話,match()就返回none。
re.match(pattern, string, flags=0)
# (標(biāo)準(zhǔn),要匹配的,標(biāo)志位)
print(re.match('www','www.xxxx.com'))
print(re.match('www','www.xxxx.com').span())
print(re.match('com','www.xxxx.com'))
<re.Match object; span=(0, 3), match='www'>
(0, 3)
None

group匹配對象

import re
a = 'life is short,i use python,i love python'
r = re.search('life(.*)python(.*)python',a)
print(r.group(0))       # 完整正則匹配 ,life is short,i use python,i love python
print(r.group(1))       # 第1個分組之間的取值 is short,i use 
print(r.group(2))       # 第2個分組之間的取值 ,i love 
print(r.group(0,1,2)) # 以元組形式返回3個結(jié)果取值 ('life is short,i use python,i love python', ' is short,i use ', ',i love ')
print(r.groups())       # 返回就是group(1)和group(2) (' is short,i use ', ',i love ')
import re
# .*        表示任意匹配除換行符(\n、\r)之外的任何單個或多個字符
# (.*?)     表示"非貪婪"模式,只保存第一個匹配到的子串
# re.M      多行匹配,影響 ^ 和 $
# re.I      使匹配對大小寫不敏感
line = "Cats are smarter than dogs"
matchObj1 = re.match(r'(.*) are (.*?) .*', line,  re.M|re.I)
matchObj2 = re.match(r'(.*) smarter (.*?) .*', line,  re.M|re.I)
matchObj3 = re.match(r'(.*) than (.*)', line,  re.M|re.I)
print(matchObj1)
print(matchObj2)
print(matchObj3)
# <re.Match object; span=(0, 26), match='Cats are smarter than dogs'>
# <re.Match object; span=(0, 26), match='Cats are smarter than dogs'>
# None
if matchObj1:
   print ("matchObj1.group() : ", matchObj1.group())
   print ("matchObj1.group(1) : ", matchObj1.group(1))
   print ("matchObj1.group(2) : ", matchObj1.group(2))
else:
   print ("No match!!")
if matchObj2:
   print ("matchObj2.group() : ", matchObj2.group())
   print ("matchObj2.group(1) : ", matchObj2.group(1))
   print ("matchObj2.group(2) : ", matchObj2.group(2))
else:
   print ("No match!!")
if matchObj3:
   print ("matchObj3.group() : ", matchObj3.group())
   print ("matchObj3.group(1) : ", matchObj3.group(1))
   print ("matchObj3.group(2) : ", matchObj3.group(2))
else:
   print ("No match!!")
# matchObj1.group() :  Cats are smarter than dogs
# matchObj1.group(1) :  Cats
# matchObj1.group(2) :  smarter
# matchObj2.group() :  Cats are smarter than dogs
# matchObj2.group(1) :  Cats are
# matchObj2.group(2) :  than
# matchObj3.group() :  Cats are smarter than dogs
# matchObj3.group(1) :  Cats are smarter
# matchObj3.group(2) :  dogs
import re
# 點 是匹配單個字符
# 星是前面的東西出現(xiàn)0次或無數(shù)次
# 點星就是任意字符出現(xiàn)0次或無數(shù)次
str = "a b a b"
matchObj1 = re.match(r'a(.*)b', str,  re.M|re.I)
matchObj2 = re.match(r'a(.*?)b', str,  re.M|re.I)
print("matchObj1.group() : ", matchObj1.group())
print("matchObj2.group() : ", matchObj2.group())
# matchObj1.group() :  a b a b
# matchObj2.group() :  a b

re.search

掃描整個字符串并返回第一個成功的匹配。

re.search(pattern, string, flags=0)
import  re
line = "cats are smarter than dogs"
matchObj = re.match(r'dogs',line,re.M|re.I)
matchObj1= re.search(r'dogs',line,re.M|re.I)
matchObj2= re.match(r'(.*) dogs',line,re.M|re.I)
if matchObj:
   print ("match --> matchObj.group() : ", matchObj.group())
else:
   print ("No match!!")
if matchObj1:
   print ("match --> matchObj1.group() : ", matchObj1.group())
else:
   print ("No match!!")
if matchObj2:
   print ("match --> matchObj2.group() : ", matchObj2.group())
else:
   print ("No match!!")
# No match!!
# match --> matchObj1.group() :  dogs
# match --> matchObj2.group() :  cats are smarter than dogs

re.compile

  • re.compile是將正則表達(dá)式轉(zhuǎn)換為模式對象
  • 這樣可以更有效率匹配。使用compile轉(zhuǎn)換一次之后,以后每次使用模式時就不用進(jìn)行轉(zhuǎn)換

三、檢索和替換

re.sub 替換字符串

re.sub('被替換的','替換成的',a)
# 把FBI替換成BBQ
import re
a = 'abcFBIabcCIAabc'
r = re.sub('FBI','BBQ',a)
print(r)
# 把FBI替換成BBQ,第4參數(shù)寫1,證明只替換第一次,默認(rèn)是0(無限替換)
import re
a = 'abcFBIabcFBIaFBICIAabc'
r = re.sub('FBI','BBQ',a,1)
print(r)
# abcBBQabcCIAabc
# abcBBQabcFBIaFBICIAabc
# 把函數(shù)當(dāng)參數(shù)傳到sub的列表里,實現(xiàn)把業(yè)務(wù)交給函數(shù)去處理,例如將FBI替換成$FBI$
import re
a = 'abcFBIabcFBIaFBICIAabc'
def 函數(shù)名(形參):
    分段獲取 = 形參.group()           # group()在正則表達(dá)式中用于獲取分段截獲的字符串,獲取到FBI
    return '$' + 分段獲取 + '$'
r = re.sub('FBI',函數(shù)名,a)
print(r)

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • Python打印異常信息的方法示例詳解

    Python打印異常信息的方法示例詳解

    在 Python 編程中,異常是指程序執(zhí)行過程中出現(xiàn)的錯誤或異常情況,當(dāng)程序遇到異常時,為了更好地調(diào)試和定位問題,我們需要打印異常信息,本文將詳細(xì)介紹如何在 Python 中打印異常,并提供一些示例和注意事項,需要的朋友可以參考下
    2023-12-12
  • Python Flask 和 Django 的區(qū)別與適用場景示例分析

    Python Flask 和 Django 的區(qū)別與適用場景示例分析

    Flask和Django是兩個流行的Python Web框架,但設(shè)計哲學(xué)、功能和用法有很大區(qū)別,Flask是一個輕量級框架,簡單靈活,適合小型項目和快速原型開發(fā),本文給大家介紹Python Flask 和 Django 的區(qū)別與適用場景示例分析,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • python中各種路徑設(shè)置的方法詳解

    python中各種路徑設(shè)置的方法詳解

    python程序想要在一個設(shè)備上去運行除了需要安裝python解釋器和相關(guān)依賴項之外,還需要將對應(yīng)的文件路徑添加到環(huán)境變量path中才可以,下面這篇文章主要給大家介紹了關(guān)于python中各種路徑設(shè)置的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Python環(huán)境下安裝PyGame和PyOpenGL的方法

    Python環(huán)境下安裝PyGame和PyOpenGL的方法

    這篇文章主要介紹了Python環(huán)境下安裝PyGame和PyOpenGL的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • python中類的屬性和方法介紹

    python中類的屬性和方法介紹

    在本篇內(nèi)容里小編給大家整理了關(guān)于python中類的屬性知識點以及使用方法介紹,需要的朋友們參考下。
    2018-11-11
  • 使用Python?Matplotlib處理地理數(shù)據(jù)可視化

    使用Python?Matplotlib處理地理數(shù)據(jù)可視化

    地理數(shù)據(jù)可視化是數(shù)據(jù)科學(xué)中一個重要的領(lǐng)域,它幫助我們理解和分析與地理位置相關(guān)的數(shù)據(jù),Python?提供了強大的工具來處理地理數(shù)據(jù),本文將介紹如何使用?Python?Matplotlib?處理地理數(shù)據(jù)可視化,包括基礎(chǔ)概念、常用庫、數(shù)據(jù)處理以及實際案例,需要的朋友可以參考下
    2024-11-11
  • 實例介紹Python中整型

    實例介紹Python中整型

    在本篇內(nèi)容中我們通過實例分享了關(guān)于Python中整型的相關(guān)知識點內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2019-02-02
  • Python批量操作Excel文件詳解

    Python批量操作Excel文件詳解

    因為博主所在的地方,需要每周整理全校的青年大學(xué)習(xí)數(shù)據(jù),Excel操作本身不難,但是這種毫無意義的體力勞動做久了就會很無趣,剛好我想起來上學(xué)期接觸過Python,想著能不能試一下,取代這種無意義的勞動
    2021-11-11
  • pytorch 模型的train模式與eval模式實例

    pytorch 模型的train模式與eval模式實例

    今天小編就為大家分享一篇pytorch 模型的train模式與eval模式實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python?數(shù)據(jù)篩選功能實現(xiàn)

    Python?數(shù)據(jù)篩選功能實現(xiàn)

    這篇文章主要介紹了Python?數(shù)據(jù)篩選,無論是在數(shù)據(jù)分析還是數(shù)據(jù)挖掘的時候,數(shù)據(jù)篩選總會涉及到,這里我總結(jié)了一下python中列表,字典,數(shù)據(jù)框中一些常用的數(shù)據(jù)篩選的方法,需要的朋友可以參考下
    2023-04-04

最新評論

厦门市| 宁乡县| 义乌市| 武邑县| 富阳市| 枣强县| 南涧| 井冈山市| 耒阳市| 马边| 武邑县| 外汇| 宁强县| 高陵县| 衡阳县| 盐山县| 田阳县| 集安市| 台中市| 宜昌市| 合肥市| 新和县| 东平县| 琼结县| 江都市| 扶绥县| 股票| 象山县| 百色市| 区。| 盐亭县| 通化县| 栖霞市| 秦安县| 江山市| 天门市| 葵青区| 彰化市| 文水县| 新巴尔虎左旗| 乐都县|