1. 程式人生 > >PYthon 正則表示式設定大小寫匹配

PYthon 正則表示式設定大小寫匹配

 

在python中經常會需要區分大小寫,在此介紹一種最常用的設定方法:

一:確定要匹配的文字

text="[email protected],[email protected],[email protected]"    #這是要進行匹配的文字內容,可以是一個檔案

步驟二:書寫正則表示式

p='([a-z0-9]+)\@([a-z0-9]+)\.([a-z]{2,4})'                                                               #這是自己寫的正則表示式

步驟三:對書寫的正則表示式進行編譯

模式一:不規定大小寫模式,那麼預設為需要區分大小寫

pattern=re.compile(p)                                                                                            #編譯

 in:

print pattern.findall(text)                                                                                     #打印出來查詢內容

 out:

[('dfdsa', 'dfdf', 'com'), ('zgabgj', 'asdfdsa', 'com')]

模式二:設定引數區分大小寫

pater=re.compile(p,flags=re.IGNORECASE)

輸出為

in:

print pattern.findall(text)                                                                                   #打印出來查詢內容

out:

 [('DFSFSDFDSF', 'qq', 'com'), ('dfdsa', 'dfdf', 'com'), ('zgabgj', 'asdfdsa', 'com')]