Python的re模組主要方法

2022-06-25 18:36:10 字數 920 閱讀 9256

complie(pattern[,flags]) 建立模式物件

search(pattern,string[,flags]) 在整個字串中尋找模式,返回match物件或none

match(pattern,string[,flags]) 從字串的開始處匹配,返回match物件或none

findall(pattern,string[,flags]) 列出字串中模式的所有匹配項

split(pattern,string[,maxsplit = 0]) 根據模式匹配項分割字串

sub(pat,repl,string[,count = 0]) 將字串中所有pat的匹配項用repl替換

escape(string) 將字串中所有特殊正規表示式字元轉義

Python的re模組常用方法

search 匹配就 返回乙個變數,通過group取匹配到的第乙個值,不匹配就返回none,group會報錯 match 相當於search的正規表示式中加了乙個 spilt 返回列表,按照正則規則切割,預設匹配到的內容會被切掉 sub subn 替換,按照正則規則去尋找要被替換掉的內容,subn返...

re模組的方法

一.re.match re.match 嘗試從字串的開始匹配乙個模式,如 下面的例子匹配第乙個單詞。import re text jgood is a handsome boy,he is cool,clever,and so on.m re.match r w s text if m print ...

python模組 re模組

匹配任意字元 匹配指定字元類別 字元開頭 字元結尾 取非字元 重複多次字元 0次或多次 重複多次字元 1次或多次 重複單次字元 左右表示式任意匹配 重複m到n次字元 重複m次字元 d 匹配任何十進位制數,相當於 0 9 d 匹配任何非數字字元,相當於 0 9 s 匹配任何空白字元,相當於 fdss ...