網站首頁 編程語言 正文
一、re是什么?
正則表達式是一個特殊的字符序列,能方便的檢查一個字符串是否與某種模式匹配。re模塊使得python擁有全部的正則表達式功能。
二、re 模塊的作用
通過使用正則表達式,可以:
測試字符串內的模式?!?例如,可以測試輸入字符串,以查看字符串內是否出現電話號碼模式或信用卡號碼模式。這稱為數據驗證。
替換文本。—— 可以使用正則表達式來識別文檔中的特定文本,完全刪除該文本或者用其他文本替換它。
基于模式匹配從字符串中提取子字符串。—— 可以查找文檔內或輸入域內特定的文本。
三、re模塊的使用
1、常用方法
-
findAll():
匹配所有的字符串,把匹配結果作為一個列表返回 -
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 匹配數字0-9
import re res2 = '[\d]' s = 'hell666o pyt999hon' result = re.findall(res2, s) print(result) # ['6', '6', '6', '9', '9', '9']
\D 匹配非數字, 包含空格
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)*:匹配前一個字符出現一次,或無限次 貪婪模式
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) + :匹配前一個字符出現1次或無窮次
import re res2 = 'h+' s = '-hhello hhh python' result = re.findall(res2, s) print(result) # ['hh', 'hhh', 'h']
(3)?: 匹配前一個字符出現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} :匹配前一個字符連續出現n次
import re res2 = 'https{2}' s = '-hhello-httpssss-python' result = re.findall(res2, s) print(result) # ['httpss'] 匹配到前一個字符s 連續出現2次
{n,m} :匹配前一個字符出現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']
$ : 匹配以哪個字符結尾的字符
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()) # 在起始位置匹配 ,返回匹配到的區間下標 (0,3) print(re.match('com', str)) # 不在起始位置匹配 None
6、 search():在字符串中搜索,返回搜索到的第一個
str = "www.runoob.com" print(re.search('www', str).span()) # 在起始位置匹配 ,返回匹配到的區間下標 print(re.search('com', str).span()) # 不在起始位置匹配
re.match只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數返回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
總結
原文鏈接:https://blog.csdn.net/qq_37982823/article/details/122489662
相關推薦
- 2022-04-05 macOS下安裝JDK11和配置環境變量
- 2022-10-19 react封裝Dialog彈框的方法_React
- 2022-12-04 Jetpack之CameraX的使用_Android
- 2022-08-16 一篇文章徹底搞懂Python切片操作_python
- 2022-05-24 Pytho的HTTP交互httpx包模塊使用詳解_python
- 2022-04-17 瀏覽器無法復制文字解決辦法
- 2022-05-14 Centos8安裝docker報錯(錯誤提示:All?mirrors?were?tried)的問題_
- 2022-12-22 如何使用python獲取現在的日期與時間_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支