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繪圖之實現(xiàn)繪制極坐標(biāo)圖像
這篇文章主要介紹了如何利用python繪制極坐標(biāo)圖像,文中的示例代碼講解詳細(xì),具有一定的的參考價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08Python如何使用print()函數(shù)輸出格式化字符串
Python中內(nèi)置的%操作符和format函數(shù),都可以用于格式化字符串,下面這篇文章主要給大家介紹了關(guān)于Python如何使用print()函數(shù)輸出格式化字符串的相關(guān)資料,需要的朋友可以參考下2021-08-08

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

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