Python筆記 re(正則)筆記

2021-10-02 20:27:21 字數 617 閱讀 5794

import re
re.findall()

返回匹配結果(列表)

re.sub()

替換字元(根據正則規則替換)

re.compile()

編譯正規表示式(當需要多次使用時更效率)

re.findall(『今天天氣(\d+)°c,多雲』, 待匹配字串)

\d+表示至少乙個數字

re.findall(『記錄常用的(.*?)筆記』, 待匹配字串)

.*表示任意字元;?表示非貪婪模式

re.findall(『遮天第一章(.*?)第一章結束』, 待匹配字串, re.s)

re.s表示多行匹配

re.findall((a|b), 待匹配字串)

| 表示或,可以匹配多個表示式

re.findall([abcd]), 待匹配字串)

[ ]表示匹配其中乙個

re_note = re.compile(''記錄常用的(.*?)筆記'')  		# 編譯為正規表示式物件

re_note.findall(待匹配的字串) # 使用findall方法匹配

——這些大多時候夠用了喲!

python 正則re學習筆記

正規表示式,又稱規則表示式 正規表示式 regular expression 描述了一種字串匹配的模式 pattern 1.代表原子表 儲存表示式 字元集合。匹配所包含的任意乙個字元 a 匹配乙個字母a abc 匹配字母a b c a z 匹配任意一位小寫字母 aabb 匹配任意一位大小寫字母a b...

re 正則表達筆記

span 跨度 pattern 模式 todo re.match todo 返回開頭匹配的結果,若開頭無匹配項,則返回none print re.match www www.runoob.com print re.match www www.runoob.com span 0,3 print re....

正規表示式re筆記

import re 1.re.match 只能從頭開始匹配 例如 ret re.match abc 123abc123abc 匹配abc失敗 ret re.match abc abc123 匹配abc成功 ret.span 返回匹配的索引範圍 0,2 ret.group 返回匹配的字元 abc 2....