網(wǎng)站首頁 編程語言 正文
1 re.match 說明
re.match()? 從開始位置開始往后查找,返回第一個符合規(guī)則的對象,如果開始位置不符合匹配隊形則返回None
從源碼里面看下match 里面的內(nèi)容
里面有3個參數(shù) pattern ,string ,flags?
pattern : 是匹配的規(guī)則內(nèi)容
string : 要匹配的字符串
flag : 標(biāo)志位(這個是可選的,可寫,可不寫),用于控制正則表達(dá)式的匹配方式,如:是否區(qū)分大小寫,多行匹配等等
下面寫一個demo
str_content = "Python is a good language" # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python" # pattern 匹配的規(guī)則
re_content = re.match("Python", str_content)
print(re_content)
打印的結(jié)果如下
可以看到匹配的的下標(biāo)是(0,6) 匹配的內(nèi)容是Python
2 span 的使用
如果想獲取匹配的下標(biāo),可以使用span ,
match span 的作用就是返回匹配到內(nèi)容的下標(biāo)
使用方式如下
import re # 導(dǎo)入re 模塊
str_content = "Python is a good language" # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python" # pattern 匹配的規(guī)則
re_content = re.match("Python", str_content).span()
print(re_content)
打印結(jié)果如下
3 group 的使用
如果想獲取匹配到結(jié)果的內(nèi)容可以使用group ,注意使用group的時候就不要在使用span 了
import re # 導(dǎo)入re 模塊
str_content = "Python is a good language" # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python" # pattern 匹配的規(guī)則
re_content = re.match("Python", str_content)
print(re_content.group())
打印結(jié)果如下
4 匹配不到內(nèi)容的情況
如下面的返回結(jié)果為None
import re # 導(dǎo)入re 模塊
str_content = "Python is a good language" # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python" # pattern 匹配的規(guī)則
re_content = re.match("python", str_content)
print(re_content)
# 或者
str_content = "Python is a good language" # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python" # pattern 匹配的規(guī)則
re_content = re.match("is", str_content)
print(re_content)
5 使用group 注意點
注意當(dāng)匹配不到內(nèi)容的時候就使用group 或者span 的時候會報錯,所以當(dāng)使用group 的時候 先判斷下是否匹配到內(nèi)容然后在使用它
例如匹配不到內(nèi)容的情況下使用group
import re # 導(dǎo)入re 模塊
str_content = "Python is a good language" # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python" # patterPn 匹配的規(guī)則
re_content = re.match("python", str_content)
print(re_content.group())
這樣會報錯,報錯內(nèi)容如下
添加是否匹配判斷
import re # 導(dǎo)入re 模塊
str_content = "Python is a good language" # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python" # patterPn 匹配的規(guī)則
re_content = re.match("python", str_content)
if re_content:
print(re_content.group())
else:
print("沒有匹配到內(nèi)容")
打印結(jié)果如下
這樣會走到else 里面就不會報錯了
6 flag 的使用
寫一個忽略大小寫的情況
import re # 導(dǎo)入re 模塊
str_content = "Python is a good language" # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python" # patterPn 匹配的規(guī)則
re_content = re.match("python", str_content, re.I)
if re_content:
print(re_content.group())
else:
print("沒有匹配到內(nèi)容")
打印結(jié)果如下:
flags?: 可選,表示匹配模式,比如忽略大小寫,多行模式等,具體參數(shù)為:
- re.I 忽略大小寫
- re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依賴于當(dāng)前環(huán)境
- re.M 多行模式
- re.S 即為 . 并且包括換行符在內(nèi)的任意字符(. 不包括換行符)
- re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依賴于 Unicode 字符屬性數(shù)據(jù)庫
- re.X 為了增加可讀性,忽略空格和 # 后面的注釋
原文鏈接:https://blog.csdn.net/qq_33210042/article/details/116794784
相關(guān)推薦
- 2022-07-19 Angular @Inject 注解的實際應(yīng)用例子和工作原理淺析
- 2022-05-11 Synchronized鎖優(yōu)化
- 2023-01-21 C++?STL中的常用遍歷算法分享_C 語言
- 2022-11-14 值類型和引用類型的區(qū)別 I 數(shù)據(jù)結(jié)構(gòu)中的堆和棧和內(nèi)存中的堆和棧的區(qū)別
- 2022-04-20 詳細(xì)聊聊sql中exists和not?exists用法_數(shù)據(jù)庫其它
- 2023-01-07 Python中層次聚類的詳細(xì)講解_python
- 2022-08-16 git中cherry-pick命令的使用教程_其它綜合
- 2022-09-20 C#?Winform實現(xiàn)圓角無鋸齒按鈕_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支