網(wǎng)站首頁 編程語言 正文
flags參數(shù)
re.I
? ? IGNORECASE
? ? 忽略字母大小寫re.L
? ? LOCALE
? ? 影響 “w, “W, “b, 和 “B,這取決于當前的本地化設(shè)置。re.M
? ? MULTILINE
? ? 使用本標志后,‘^’和‘$’匹配行首和行尾時,會增加換行符之前和之后的位置。re.S
? ? DOTALL
? ? 使 “.” 特殊字符完全匹配任何字符,包括換行;沒有這個標志, “.” 匹配除了換行符外的任何字符。re.X
? ? VERBOSE
? ? 當該標志被指定時,在 RE 字符串中的空白符被忽略,除非該空白符在字符類中或在反斜杠之后。
? ? 它也可以允許你將注釋寫入 RE,這些注釋會被引擎忽略;
? ? 注釋用 “#”號 來標識,不過該符號不能在字符串或反斜杠之后。
忽略大小寫
import re text = '我愛Python我愛python' pat1 = 'p' # search r1 = re.findall(pattern=pat1, string=text, flags=re.I) print(r1)
[‘P’, ‘p’]
多行模式
import re text = '我愛數(shù)學(xué)\n我愛Python\n我愛python' pat1 = '^我' # search r1 = re.findall(pattern=pat1, string=text) r2 = re.findall(pattern=pat1, string=text, flags=re.M) print(r1) print(r2)
[‘我’]
[‘我’, ‘我’, ‘我’]
匹配任何字符
import re text = ''' 我愛Python 我愛pandas ''' pat1 = '.我' # search r1 = re.findall(pattern=pat1, string=text, flags=re.S) print(r1) r2 = re.findall(pattern=pat1, string=text) print(r2)
[’\n我’, ‘\n我’]
[]
補充:正則表達式中的flags
MULTILINE,多行模式, 改變 ^ 和 $ 的行為
In [63]: s Out[63]: 'first line\nsecond line\nthird line' In [64]: pattern=re.compile(r'^\w+') In [65]: re.findall(pattern,s) Out[65]: ['first'] In [67]: pattern=re.compile(r'^\w+',re.M) In [68]: re.findall(pattern,s) Out[68]: ['first', 'second', 'third']
re.S ? DOTALL,此模式下 '.' 的匹配不受限制,可匹配任何字符,包括換行符,也就是默認是不能匹配換行符
In [62]: s = '''first line ...: second line ...: third line''' In [71]: regex=re.compile('.+',re.S) In [73]: regex.findall(s) Out[73]: ['first line\nsecond line\nthird line'] In [74]: regex=re.compile('.+') In [75]: regex.findall(s) Out[75]: ['first line', 'second line', 'third line']
re.X ? ?VERBOSE,冗余模式, 此模式忽略正則表達式中的空白和#號的注釋
email_regex = re.compile("[\w+\.]+@[a-zA-Z\d]+\.(com|cn)") email_regex = re.compile("""[\w+\.]+ # 匹配@符前的部分 @ # @符 [a-zA-Z\d]+ # 郵箱類別 \.(com|cn) # 郵箱后綴 """, re.X)
總結(jié)
原文鏈接:https://blog.csdn.net/Yellow_python/article/details/80543937
相關(guān)推薦
- 2022-01-06 react實現(xiàn)todolist的增刪改查
- 2023-07-03 利用ant-design下拉選擇框select的labelInValue屬性給下拉選項添加圖標
- 2022-03-14 如何在Git hub上快速查找合適的項目(如何在github上找到自己想要的代碼)
- 2023-03-20 C#中如何限制TextBox控件內(nèi)輸入值的范圍_C#教程
- 2022-10-20 C++淺析虛函數(shù)使用方法_C 語言
- 2022-07-09 Android開發(fā)中Flutter組件實用技巧_Android
- 2023-02-01 Python動態(tài)演示旋轉(zhuǎn)矩陣的作用詳解_python
- 2022-05-21 一次線上mongo慢查詢問題排查處理記錄_MongoDB
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細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之認證信息的處理
- Spring Security之認證過濾器
- 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被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支