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():匹配字符串的開始位置,如果開始位置沒有,則返回Nonesearch():在字符串中搜索,返回搜索到的第一個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根據(jù)list重命名文件夾里的所有文件實例
今天小編就為大家分享一篇python根據(jù)list重命名文件夾里的所有文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python實現(xiàn)ssh及sftp功能(實例代碼)
這篇文章主要介紹了python實現(xiàn)ssh及sftp功能 ,本文分步驟通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Python之tkinter面板PanedWindow的使用
這篇文章主要介紹了Python之tkinter面板PanedWindow的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
vim自動補全插件YouCompleteMe(YCM)安裝過程解析
這篇文章主要介紹了vim自動補全插件YouCompleteMe(YCM)安裝過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
python把數(shù)據(jù)導(dǎo)出生成excel文件的方法小結(jié)
在Python中,將數(shù)據(jù)導(dǎo)出生成Excel文件,最常用的庫之一是openpyxl(用于處理.xlsx文件)和pandas,以下將分別介紹如何使用這兩個庫來生成Excel文件,感興趣的小伙伴跟著小編一起來看看吧2024-09-09

