1. 程式人生 > >Python3 re(正則表示式)

Python3 re(正則表示式)

#coding=utf-8
# regular.py 正則表示式
import re # 正則模組

def regular():
    data = "She is more than pretty. 520"

    # --- 正則 ---
    reg = r"mo" # 指定字元 => span=(7, 9), match='mo'
    reg = r"." # (.)單個字元 => span=(0, 1), match='S'
    reg = r"\." # (\)轉義符 => span=(23, 24), match='.'
    reg = r"[.]"
# ([])字元集合(注意:部分特殊字元失去特殊意義) => span=(23, 24), match='.' reg = r"[love]" # []內任意字元 => span=(2, 3), match='e' reg = r"[i-u]" # (-)範圍 => span=(4, 5), match='i' reg = r"t{2}" # {}內為長度(3個6) => span=(20, 22), match='tt' reg = r"t{1,3}" # {M,} / {.N} / {N} => span=(12, 13), match='t'
reg = r"(i|o|u){1}" # (())組 => span=(4, 5), match='i' reg = r"^S" # (^)開頭 => span=(0, 1), match='S' reg = r"[^S]" # ([^])取反(不含H) => span=(1, 2), match='h' reg = r"520$" # ($)結尾 => span=(25, 28), match='520' reg = r"et*" # (*)匹配{0,}個表示式 => ['e', 'e', 'ett'] reg = r"et+"
# (+)匹配{1,}個表示式 => ['ett'] reg = r"et?" # (?)匹配{0,1}個表示式 => ['e', 'e', 'et'] reg = r".+?e" # (?)非貪婪模式(span=(0, 20), match='She is more than pre' => span=(0, 3), match='She') reg = r"\145" # ascii標的8進位制數(145=101=e) => span=(2, 3), match='e' reg = r"\d" # (\d)單個數字 => span=(25, 26), match='5' (推薦:[0-9]) reg = r"\D" # (\D)非數字 => span=(0, 1), match='S' (推薦:[^0-9]) reg = r"\s" # (\s)空白字元 => span=(3, 4), match=' ' (推薦:[\t\n\r\f\v]) reg = r"\S" # (\S)非空白字元 => span=(0, 1), match='S' (推薦:[^\t\n\r\f\v]) reg = r"\w" # (\w)單詞 => span=(0, 1), match='S' (推薦:[a-zA-Z0-9_]) reg = r"\W" # (\W)非單詞 => span=(3, 4), match=' ' (推薦:[^a-zA-Z0-9_]) reg = r"\AS" # (\A)開頭 => span=(0, 1), match='S' reg = r"520\Z" # (\Z)結尾 => span=(25, 28), match='520' reg = r"y\b" # (\b)單詞邊界(Hello) => span=(22, 23), match='y' reg = r"o\B" # (\B)非單詞邊界(world) => span=(8, 9), match='o' reg = r"[01]\d\d|2[0-4]\d|25[0-5]" # 或(|) 多位數(匹配0 - 255 直接的數字) index = re.search(reg, data) # 查詢單個匹配項 index = re.match(r"She", data) # 匹配開頭 => span=(0, 3), match='She' index = re.fullmatch(r".+", data) # 匹配全部 => span=(0, 28), match='She is more than pretty. 520' lists = re.findall(reg, data) # 查詢所有匹配項(列表) lists = re.split(r"o", data, maxsplit=1) # 根據正則分割字串(maxsplit分割次數) => ['She is m', 're than pretty. 520'] strs = re.sub(r"\.", r"!", data, count=1) # 替換(count:替換次數)(匹配替換,未匹配原樣) => She is more than pretty! 520 re.purge() # 清除正則表示式快取 # --- 正則表示式物件 --- pat = re.compile(r"e") # 編譯成正則物件 index = pat.search(data) # 查詢單個匹配項 => span=(2, 3), match='e' index = pat.search(data, 5) # => span=(10, 11), match='e' index = pat.search(data, 1, 10) index = pat.match(data) # 匹配開頭 => None index = pat.match(data, 2) # => span=(2, 3), match='e' index = pat.match(data, 1, 10) index = pat.fullmatch(data) # 匹配全部 => None index = pat.fullmatch(data, 2) # => None index = pat.fullmatch(data, 2, 3) # span=(2, 3), match='e' lists = pat.split(data, maxsplit=0) # 分割 => ['Sh', ' is mor', ' than pr', 'tty. 520'] lists = pat.findall(data) # 查詢全部 => ['e', 'e', 'e'] lists = pat.findall(data, 5) # => ['e', 'e'] lists = pat.findall(data, 1, 10) # => ['e'] strs = pat.sub(r"o", data, count=0) # 替換 => Sho is moro than protty. 520 # --- Match --- match = index; # span=(2, 3), match='e' strs = match.string # 被匹配的資料 => She is more than pretty. 520 strs = match.group() # 獲取 match 資料 => e pos = match.pos # => 2 pos = match.endpos # => 3 if __name__ == "__main__": regular()