欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python自動化之re模塊詳解

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

一、re是什么?

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

二、re 模塊的作用

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

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

(6) | :兩個條件進(jìn)行匹配,或的關(guān)系

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ū)間下標(biāo)  (0,3)
print(re.match('com', str))  # 不在起始位置匹配  None

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

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

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

7、 finditer():

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

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

總結(jié)

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

相關(guān)文章

  • python中樹與樹的表示知識點總結(jié)

    python中樹與樹的表示知識點總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于python中樹與樹的表示的相關(guān)知識點,需要的讀者們學(xué)習(xí)下吧。
    2019-09-09
  • python將時分秒轉(zhuǎn)換成秒的實例

    python將時分秒轉(zhuǎn)換成秒的實例

    今天小編就為大家分享一篇python將時分秒轉(zhuǎn)換成秒的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python爬蟲輔助利器PyQuery模塊的安裝使用攻略

    Python爬蟲輔助利器PyQuery模塊的安裝使用攻略

    這篇文章主要介紹了Python爬蟲輔助利器PyQuery模塊的安裝使用攻略,PyQuery可以方便地用來解析HTML內(nèi)容,使其成為眾多爬蟲程序開發(fā)者的大愛,需要的朋友可以參考下
    2016-04-04
  • Django集成MongoDB實現(xiàn)過程解析

    Django集成MongoDB實現(xiàn)過程解析

    這篇文章主要介紹了Django集成MongoDB實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • Python生成驗證碼實例

    Python生成驗證碼實例

    這篇文章主要介紹了Python生成驗證碼的方法,具有很好的實用價值,代碼結(jié)構(gòu)清晰易懂,需要的朋友可以參考下
    2014-08-08
  • Pycharm連接遠(yuǎn)程mysql報錯的實現(xiàn)

    Pycharm連接遠(yuǎn)程mysql報錯的實現(xiàn)

    本文主要介紹了Pycharm連接遠(yuǎn)程mysql報錯的實現(xiàn),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • 用Python的Tornado框架結(jié)合memcached頁面改善博客性能

    用Python的Tornado框架結(jié)合memcached頁面改善博客性能

    這篇文章主要介紹了用Python的Tornado框架結(jié)合memcached頁面改善vLog性能,主要使用到了緩存來提升性能,需要的朋友可以參考下
    2015-04-04
  • 最新評論