天天看點

python正規表達式手記

----------re子產品進行正則的使用---------- #result=re.match(正規表達式,要比對的字元串):使用正則對字元串進行過濾從前面開始比對

#result.group():将獲得到的資料取出

#result=re.search(正規表達式,要比對的字元串):使用正則對字元串進行過濾從後面開始比對

#result==None:判斷正規表達式是否擷取到内容,如果為True,則沒有擷取到内容

#re.search(r'\d+','my hight 177 cm').group():使用正則讀字元串進行過濾從找到符合要求的字元開始比對。

#re.findall(r'\d+','my hight 177 cm my weight 100 kg'):擷取字元串中所有符合正則條件的資料資訊,并儲存到一個清單中

#re.sub(r'\d+','100','my high 177 cm'):擷取字元串中所有符合正則條件的資料資訊,并使用第二個位置上的資料資訊對其進行資料的替換操作

第二個位置可以配合函數進行處理,return傳回值為str類型

#re.split(r':| ','address:beijing [email protected]'):根據正則表示式提供的規則對字元串進行有效的切割操作。并将結果存儲到對應的清單中 ----------正規表達式單字元比對---------- .:比對任意字元

[]:比對[]中列舉的字元

\d:比對任意一個數字

\D:比對非數字,即不是數字

\s;比對空吧即 空格、tab鍵

\S:比對非空白

\w:比對單詞字元,字母、數字、下劃線

\W:比對非單詞字元,字母、數字、下劃線

----------正規表達式多個字元比對----------

*:比對前一個字元出現0次或者無限次,即可有可無

+:比對前一個字元出現1次或者無限次,即隻有有1次

?:比對前一個字元出現1次或者0次,即要麼有1次,要麼沒有

{m}:比對前一個字元出現m次

{m,n}:比對前一個字元出現從m到n次 ---------比對開頭結尾----------

^:比對字元串開頭

$:比對字元串結尾

\:轉義 ---------比對分組----------

|:比對左右任意一個表達式

():将括号中字元作為一個分組

\<num>:引用分組num比對到的字元串

(?P<name>):分組起别名

(?P=name):引用别名為name分組比對到的字元串   ---------附:正規表達式的練習題--------- 1、比對網址 有一批網址: http://www.interoem.com/messageinfo.asp?id=35

http://3995503.com/class/class09/news_show.asp?id=14

http://lib.wzmc.edu.cn/news/onews.asp?id=769

http://www.zy-ls.com/alfx.asp?newsid=377&id=6

http://www.fincm.com/newslist.asp?id=415 需要 正則後為: http://www.interoem.com/ 

http://3995503.com/ 

http://lib.wzmc.edu.cn/ 

http://www.zy-ls.com/ 

http://www.fincm.com/ 代碼實作:

1 def testFirst():
 2     #要進行處理的資料
 3     strHtml='http://www.interoem.com/messageinfo.asp?id=35 [url]http://3995503.com/class/class09/news_show.asp?id=14[/url] [url]http://lib.wzmc.edu.cn/news/onews.asp?id=769[/url] [url]http://www.zy-ls.com/alfx.asp?newsid=377&id=6[/url] [url]http://www.fincm.com/newslist.asp?id=415'[/url]
 4    
 5     #strHtml='http://www.interoem.com/messageinfo.asp?id=35'
 6    
 7
 8     print("轉化前對應的資料:%s"%strHtml)
 9
10     #程序正在表達式處理
11     result=re.findall("(http://.*?\.(com|cn)/)",strHtml)
12
13     #測試
14     #result = re.match("http://.*\.(com|cn)/",strHtml).group()
15
16     #建立一個變量,進行結果的存儲
17     strResult=''
18
19     #變量結果
20     for item in result:
21         strResult+=item[0]+" "
22
23     #列印出結果
24     print ("轉化後對應的資料:%s"%strResult)
           

2、 比對所有合法的Python辨別符 實作代碼:

1 #引用對應的包
 2 import re
 3
 4 import keyword
 5
 6 #2. 比對所有合法的Python辨別符
 7 def testFive():
 8     #擷取到python中關鍵字的清單
 9     keyList=keyword.kwlist
10
11     strKey="("+'|'.join(keyList)+")"
12
13     #擷取待處理的資料
14     strTitle="int main str wfwfwfwfdsfstr andand ifwhile"
15
16     #列印待處理的資料
17     print("處理前的資料:%s"%strTitle)
18    
19     #進行正則的處理
20     result=re.findall(strKey,strTitle)
21    
22     #列印處理後的資料
23     print ("處理後的資料:%s"%str(result))
           

3、比對合法的ip位址 代碼實作:  

1 引用包
 2 import re
 3
 4 #3. 比對合法的ip位址
 5 def testSex():
 6     #接受使用者輸入的ip位址
 7     strTitle=raw_input("請輸入要進行判斷的ip位址:")
 8
 9     strRe=''
10     strRe+='([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#第一位
11     strRe+='\.'
12     strRe+='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#第二位
13     strRe+='\.'
14     strRe+='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'#第三位
15     strRe+='\.'
16     strRe+='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'#第四位
17    
18     #進行ip是否合法的判斷
19     result=re.match(strRe,strTitle)
20
21     if result==None:
22         print("比對失敗!")
23     else:
24         print("比對成功!")
           

繼續閱讀