網站首頁 編程語言 正文
1 re.match 說明
re.match()? 從開始位置開始往后查找,返回第一個符合規則的對象,如果開始位置不符合匹配隊形則返回None
從源碼里面看下match 里面的內容
里面有3個參數 pattern ,string ,flags?
pattern : 是匹配的規則內容
string : 要匹配的字符串
flag : 標志位(這個是可選的,可寫,可不寫),用于控制正則表達式的匹配方式,如:是否區分大小寫,多行匹配等等
下面寫一個demo
str_content = "Python is a good language" # 要匹配的內容, 對應match 里面的string
str_pattern = "Python" # pattern 匹配的規則
re_content = re.match("Python", str_content)
print(re_content)
打印的結果如下
可以看到匹配的的下標是(0,6) 匹配的內容是Python
2 span 的使用
如果想獲取匹配的下標,可以使用span ,
match span 的作用就是返回匹配到內容的下標
使用方式如下
import re # 導入re 模塊
str_content = "Python is a good language" # 要匹配的內容, 對應match 里面的string
str_pattern = "Python" # pattern 匹配的規則
re_content = re.match("Python", str_content).span()
print(re_content)
打印結果如下
3 group 的使用
如果想獲取匹配到結果的內容可以使用group ,注意使用group的時候就不要在使用span 了
import re # 導入re 模塊
str_content = "Python is a good language" # 要匹配的內容, 對應match 里面的string
str_pattern = "Python" # pattern 匹配的規則
re_content = re.match("Python", str_content)
print(re_content.group())
打印結果如下
4 匹配不到內容的情況
如下面的返回結果為None
import re # 導入re 模塊
str_content = "Python is a good language" # 要匹配的內容, 對應match 里面的string
str_pattern = "Python" # pattern 匹配的規則
re_content = re.match("python", str_content)
print(re_content)
# 或者
str_content = "Python is a good language" # 要匹配的內容, 對應match 里面的string
str_pattern = "Python" # pattern 匹配的規則
re_content = re.match("is", str_content)
print(re_content)
5 使用group 注意點
注意當匹配不到內容的時候就使用group 或者span 的時候會報錯,所以當使用group 的時候 先判斷下是否匹配到內容然后在使用它
例如匹配不到內容的情況下使用group
import re # 導入re 模塊
str_content = "Python is a good language" # 要匹配的內容, 對應match 里面的string
str_pattern = "Python" # patterPn 匹配的規則
re_content = re.match("python", str_content)
print(re_content.group())
這樣會報錯,報錯內容如下
添加是否匹配判斷
import re # 導入re 模塊
str_content = "Python is a good language" # 要匹配的內容, 對應match 里面的string
str_pattern = "Python" # patterPn 匹配的規則
re_content = re.match("python", str_content)
if re_content:
print(re_content.group())
else:
print("沒有匹配到內容")
打印結果如下
這樣會走到else 里面就不會報錯了
6 flag 的使用
寫一個忽略大小寫的情況
import re # 導入re 模塊
str_content = "Python is a good language" # 要匹配的內容, 對應match 里面的string
str_pattern = "Python" # patterPn 匹配的規則
re_content = re.match("python", str_content, re.I)
if re_content:
print(re_content.group())
else:
print("沒有匹配到內容")
打印結果如下:
flags?: 可選,表示匹配模式,比如忽略大小寫,多行模式等,具體參數為:
- re.I 忽略大小寫
- re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依賴于當前環境
- re.M 多行模式
- re.S 即為 . 并且包括換行符在內的任意字符(. 不包括換行符)
- re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依賴于 Unicode 字符屬性數據庫
- re.X 為了增加可讀性,忽略空格和 # 后面的注釋
原文鏈接:https://blog.csdn.net/qq_33210042/article/details/116794784
相關推薦
- 2022-08-06 Python?pandas庫中isnull函數使用方法_python
- 2022-07-09 Python?操作?Excel?之?openpyxl?模塊_python
- 2022-04-09 WPF使用ValidationRules對MVVM架構數據驗證_基礎應用
- 2022-11-06 React?Hooks--useEffect代替常用生命周期函數方式_React
- 2022-11-03 C#事件中關于sender的用法解讀_C#教程
- 2022-09-21 Android開發兩個activity之間傳值示例詳解_Android
- 2022-10-13 Python線性表種的單鏈表詳解_python
- 2023-10-31 springboot 線程池參數解釋
- 最近更新
-
- 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同步修改后的遠程分支