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

python自動化之re模塊詳解

 更新時間:2022年01月15日 14:35:44   作者:FamilyYan  
這篇文章主要為大家介紹了python自動化之re模塊,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

一、re是什么?

正則表達式是一個特殊的字符序列,能方便的檢查一個字符串是否與某種模式匹配。re模塊使得python擁有全部的正則表達式功能。

二、re 模塊的作用

通過使用正則表達式,可以:
測試字符串內(nèi)的模式。—— 例如,可以測試輸入字符串,以查看字符串內(nèi)是否出現(xiàn)電話號碼模式或信用卡號碼模式。這稱為數(shù)據(jù)驗證。
替換文本。—— 可以使用正則表達式來識別文檔中的特定文本,完全刪除該文本或者用其他文本替換它。
基于模式匹配從字符串中提取子字符串。—— 可以查找文檔內(nèi)或輸入域內(nèi)特定的文本。

三、re模塊的使用

1、常用方法

  • findAll(): 匹配所有的字符串,把匹配結(jié)果作為一個列表返回
  • match(): 匹配字符串的開始位置,如果開始位置沒有,則返回None
  • search():在字符串中搜索,返回搜索到的第一個
  • finditer():匹配所有的字符串,返回迭代器

2、 元字符

匹配任意字符(除\n以外) h. 代表匹配h后的任意一個字符

import re
res = 'h.'
s = 'hello python'
result = re.findall(res, s)
print(result)  # ['he', 'ho']

[] 拿[]中的人任意一個字符,去字符串中匹配,匹配到一個返回一個,最后以列表返回

import re
res2 = '[hon]'
s = 'hello python'
result = re.findall(res2, s)
print(result)  # ['h', 'o', 'h', 'o', 'n']

\d 匹配數(shù)字0-9

import re
res2 = '[\d]'
s = 'hell666o pyt999hon'
result = re.findall(res2, s)
print(result)  # ['6', '6', '6', '9', '9', '9']

\D 匹配非數(shù)字, 包含空格

import re
res2 = '[\D]'
s = 'hello 3334 python 88'
result = re.findall(res2, s)
print(result)  # ['h', 'e', 'l', 'l', 'o', ' ', ' ', 'p', 'y', 't', 'h', 'o', 'n', ' ']

‘\s’ 匹配空白字符

import re
res2 = '[\s]'
s = 'hello 3334 python 88'
result = re.findall(res2, s)
print(result)  # [' ', ' ', ' ']

‘\S’ 匹配非空白字符

import re
res2 = '[\S]'
s = 'hello 3334 python 88'
result = re.findall(res2, s)
print(result)  # ['h', 'e', 'l', 'l', 'o', '3', '3', '3', '4', 'p', 'y', 't', 'h', 'o', 'n', '8', '8']

\w 匹配非特殊字符,即a-z、A-Z、0-9、_、漢字

import re
res2 = '[\w]'
s = 'hello#&_ aa 8python中國'
result = re.findall(res2, s)
print(result)  # ['h', 'e', 'l', 'l', 'o', '_', 'a', 'a', '8', 'p', 'y', 't', 'h', 'o', 'n', '中', '國']

\W 匹配特殊字符 ( - ~@#$&*)空格也屬于特殊字符

import re
res2 = '[\W]'
s = '-hello#&_ aa 8python中國'
result = re.findall(res2, s)
print(result)  # ['-', '#', '&', ' ', ' ']

3、多字符匹配

(1)*:匹配前一個字符出現(xiàn)一次,或無限次 貪婪模式

import reres2 = 'h*'s = '-hhello hhh python'result = re.findall(res2, s)print(result)  #['', 'hh', '', '', '', '', '', 'hhh', '', '', '', '', 'h', '', '', '']import re
res2 = 'h*'
s = '-hhello hhh python'
result = re.findall(res2, s)
print(result)  #['', 'hh', '', '', '', '', '', 'hhh', '', '', '', '', 'h', '', '', '']

(2) + :匹配前一個字符出現(xiàn)1次或無窮次

import re
res2 = 'h+'
s = '-hhello hhh python'
result = re.findall(res2, s)
print(result) # ['hh', 'hhh', 'h']

(3)?: 匹配前一個字符出現(xiàn)0次或者1次,非貪婪模式

import re
res2 = 'h?'
s = '-hhello hhh python'
result = re.findall(res2, s)
print(result) # ['', 'h', 'h', '', '', '', '', '', 'h', 'h', 'h', '', '', '', '', 'h', '', '', '']

(4) {n} :匹配前一個字符連續(xù)出現(xiàn)n次

import re
res2 = 'https{2}'
s = '-hhello-httpssss-python'
result = re.findall(res2, s)
print(result) # ['httpss'] 
匹配到前一個字符s 連續(xù)出現(xiàn)2次

{n,m} :匹配前一個字符出現(xiàn)n-m次

import re
res2 = 'https{1,3}'
s = '-hhello-httpssss-python'
result = re.findall(res2, s)
print(result) # ['httpss']

(5) 貪婪模式和非貪婪模式

正則表達式通常使用于查找匹配字符串。貪婪模式,總是嘗試匹配盡可能多的字符;非貪婪模式正好相反,總是嘗試匹配盡可能少的字符。在"*","?","+","{m,n}"后面加上?,使貪婪變成非貪婪。

(6) | :兩個條件進行匹配,或的關系

import re
res2 = 'he|ll'
s = 'hello python'
result = re.findall(res2, s)
print(result) # ['he', 'll']

(7)邊界值:

^ :匹配以哪個字符開頭的

import re
res2 = '^he'
s = 'hello python'
result = re.findall(res2, s)
print(result) # ['he']

$ : 匹配以哪個字符結(jié)尾的字符

import re
res2 = 'on$'
s = 'hello python'
result = re.findall(res2, s)
print(result) # ['on']

4、分組匹配

() :只匹配()里面的

import re
res2 = '#(\w.+?)#'
s = "{'mobile_phone':'#mobile_phone#','pwd':'Aa123456'}"
result = re.findall(res2, s)
print(result)  # ['mobile_phone']

5、match()方法的使用

str = "www.runoob.com"
print(re.match('www', str).span())  # 在起始位置匹配 ,返回匹配到的區(qū)間下標  (0,3)
print(re.match('com', str))  # 不在起始位置匹配  None

6、 search():在字符串中搜索,返回搜索到的第一個

str = "www.runoob.com"
print(re.search('www', str).span())  # 在起始位置匹配 ,返回匹配到的區(qū)間下標
print(re.search('com', str).span())  # 不在起始位置匹配

re.match只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數(shù)返回None;而re.search匹配整個字符串,直到找到一個匹配。

7、 finditer():

匹配所有的字符串,返回迭代器和 findall 類似,在字符串中找到正則表達式所匹配的所有子串,并把它們作為一個迭代器返回。

res = 'h.'
s = 'hello python'
result = re.finditer(res, s)
for str in result:
    print(str.group())
he
ho

總結(jié)

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

相關文章

  • python根據(jù)list重命名文件夾里的所有文件實例

    python根據(jù)list重命名文件夾里的所有文件實例

    今天小編就為大家分享一篇python根據(jù)list重命名文件夾里的所有文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python實現(xiàn)ssh及sftp功能(實例代碼)

    python實現(xiàn)ssh及sftp功能(實例代碼)

    這篇文章主要介紹了python實現(xiàn)ssh及sftp功能 ,本文分步驟通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Django項目實戰(zhàn)之用戶頭像上傳與訪問的示例

    Django項目實戰(zhàn)之用戶頭像上傳與訪問的示例

    這篇文章主要介紹了Django項目實戰(zhàn)之用戶頭像上傳與訪問的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Python之tkinter面板PanedWindow的使用

    Python之tkinter面板PanedWindow的使用

    這篇文章主要介紹了Python之tkinter面板PanedWindow的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • vim自動補全插件YouCompleteMe(YCM)安裝過程解析

    vim自動補全插件YouCompleteMe(YCM)安裝過程解析

    這篇文章主要介紹了vim自動補全插件YouCompleteMe(YCM)安裝過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • python把數(shù)據(jù)導出生成excel文件的方法小結(jié)

    python把數(shù)據(jù)導出生成excel文件的方法小結(jié)

    在Python中,將數(shù)據(jù)導出生成Excel文件,最常用的庫之一是openpyxl(用于處理.xlsx文件)和pandas,以下將分別介紹如何使用這兩個庫來生成Excel文件,感興趣的小伙伴跟著小編一起來看看吧
    2024-09-09
  • Python四大金剛之集合詳解

    Python四大金剛之集合詳解

    這篇文章主要介紹了Python的集合,小編覺得這篇文章寫的還不錯,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10
  • Python IDLE或shell中切換路徑的操作

    Python IDLE或shell中切換路徑的操作

    這篇文章主要介紹了Python IDLE或shell中切換路徑的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python  logging日志打印過程解析

    python logging日志打印過程解析

    這篇文章主要介紹了python logging日志打印過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • Python之打印日志庫(logging)

    Python之打印日志庫(logging)

    這篇文章主要介紹了Python之打印日志庫(logging),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論

贵阳市| 疏附县| 定南县| 新郑市| 上犹县| 犍为县| 桐乡市| 东乡县| 额敏县| 焉耆| 盱眙县| 仁化县| 阿克陶县| 蕲春县| 商河县| 田东县| 尼木县| 吉首市| 德格县| 德化县| 微山县| 和龙市| 全南县| 方山县| 台江县| 桦川县| 定南县| 扶沟县| 清水河县| 辽宁省| 肃南| 加查县| 兴业县| 驻马店市| 阿荣旗| 韩城市| 丹江口市| 阳泉市| 湛江市| 岳池县| 伊通|