Python re 模組
Python 標準函式庫 re 把模式交給引擎執行。日常只要記住六個入口:search、match、fullmatch、findall/finditer、sub、split;同一個模式要用很多次再 compile。
沒有匹配時,search / match / fullmatch 回傳 None,不要直接對結果呼叫 .group()。
子頁
- search、match、fullmatch:差在「從哪裡開始、要不要整串」
- findall 與 finditer:找全部、迭代 Match 物件
- sub 與 split:取代與切開
- compile 與旗標:
re.Ire.Mre.Sre.Xre.ASCII
寫模式時請用 r"..."。行為以官方文件為準;本站範例針對 CPython 3.12+。