python re 正則表達式[轉]

01.#!/usr/bin/env python    
02.# -*- coding: utf-8 -*-    
03.import re   
04.    
05.def regex():  
06.    str = 'abcdab'   
07.    patstr = 'ab'     
08.    ##能夠匹配的2種方式:1         
09.    patobj = re.compile(patstr)  
10.    got = patobj.match(str)  
11.    ##2  
12.    got = re.match(patstr,str)  
13.         
14.    ##幾個基本的匹配函數  
15.    result = re.match('ab','abcd')   #match方法基本等同於re.search('^ab','abcd'),即默認就是前置匹配,只匹配字串的開頭  
16.    if result:  
17.        print result.group()  
18.    else:  
19.        print result  
20.  
21.    result = patobj.match('0abcd')  
22.    if result:  
23.        print result.group()  
24.    else:  
25.        print result, 'There is no result'  
26.          
27.    result = patobj.search('0abcd')  ##匹配模式並返回第一個匹配對象  
28.    if result:  
29.        print result.group()  
30.    else:  
31.        print result   
32.          
33.    str = 'abcdab'   
34.    result = patobj.findall(str)  ##返回一個包含全部匹配結果的列表,若是匹配字串中有分組的話,則返回由分組內容組成的元組所組成的列表  
35.    if result:                    ##即無分組時返回有多個group()結果的列表,有分組時返回有多個groups()結果的列表, 見下  
36.        print type(result), result  
37.    else:  
38.        print result       
39.          
40.    result = patobj.finditer(str)  ##返回一個包含全部匹配結果的迭代器,能夠配合findall使用  
41.    if result:  
42.        print type(result), result.next().group()  
43.    else:  
44.        print result  
45.  
46.    result = patobj.sub('__','abcdab')  ##用指定的字符替換全部匹配到的字符串  
47.    if result:  
48.        print 'replace:',result    ##__cd__  
49.    else:  
50.        print result  
51.          
52.    result = patobj.subn('__','abcdab')  ##用指定的字符替換全部匹配到的字符串,還包括替換數目  
53.    if result:  
54.        print 'replace:',result   ##('__cd__', 2)  
55.    else:  
56.        print result  
57.          
58.      
59.    ##基本的幾個結果查詢方法:  
60.    str = '[1]aaaa[2]bbbb[3]cccc@1[:]dddd'  
61.    result = re.search(r'\[(\d+\](.*)@1\[(.*))\]',str)  
62.    if result:  
63.        print result.group()   ##返回匹配到的第一個完整字符串: [1]aaaa[2]bbbb[3]cccc@1[:]  
64.        print result.group(1)  ##返回匹配的字符串中的第一個分組,就是第一個左擴弧和其對應的右擴弧中的全部包含的全部內容. 1]aaaa[2]bbbb[3]cccc@1[:  
65.        print result.group(2)  ##aaaa[2]bbbb[3]cccc  
66.        print result.group(3)  #最大爲3,由於匹配字串裏只有3個擴弧, :  
67.        print result.groups()  ###把全部擴弧分組的內容放在一個元組對象中,並返回:('1]aaaa[2]bbbb[3]cccc@1[:', 'aaaa[2]bbbb[3]cccc', ':')  
68.    else:  
69.        print result     
70.          
71.          
72.    ##幾個基本的匹配方式:貪婪與非貪婪  
73.    str = '[1]aaaa[2]bbbb[3]cccc@1[:]dddd'  
74.    result = re.search(r'\[.*\]',str)   
75.    if result:  
76.        print result.group()     ##[1]aaaa[2]bbbb[3]cccc@1[:]  
77.    else:  
78.        print result  
79.      
80.    str = '[1]aaaa[2]bbbb[3]cccc@1[:]dddd'  
81.    result = re.search(r'\[.*?\]',str)   ###用一個?來控制貪婪  
82.    if result:  
83.        print result.group()     ##[1]                
84.    else:  
85.        print result    
86.          
87.      
88.    ##其它的基本匹配和全部的語言都同樣,通用的,除了一些高級的用法,不過能夠參考官方手冊中的樣例,若是有須要的話  
89.             
90.                
91.if __name__ == '__main__':    
92.    regex()   

http://blog.csdn.net/five3/article/details/7068594python

 

最近用到了幾個標識的參數,好比:忽略大小寫,多行搜索等。這裏補充一下:正則表達式

有哪些標識能夠使用?函數

  • re.I(re.IGNORECASE): 忽略大小寫(括號內是完整寫法,下同)
  • re.M(re.MULTILINE):  多行模式,改變'^'和'$'的行爲(參見上圖)
  • re.S(re.DOTALL):  點任意匹配模式,改變'.'的行爲,設置後能夠匹配\n
  • re.L(re.LOCALE):  使預約字符類 \w \W \b \B \s \S 取決於當前區域設定
  • re.U(re.UNICODE):  使預約字符類 \w \W \b \B \s \S \d \D 取決於unicode定義的字符屬性
  • re.X(re.VERBOSE):  詳細模式。這個模式下正則表達式能夠是多行,忽略空白字符,並能夠加入註釋。

哪些函數支持這些標識?spa

  • re.compile(pat, string, flag=0)
  • re.findall(pat, string, flag=0)
  • re.match(pat, string, flag=0)
  • re.search(pat, string, flag=0)
其中flag就是使用標識的地方,能夠替換爲上述的標識,若是要使用多個標識,則格式爲:re.I|re.M|re.S|...
 
這裏要注意說明的是:re.M多行只能影響^和$匹配,不會影響跨行的內容匹配,好比:
 
01.str1 = "ab12\nbdc"  
02.pat1 = "^a.*2$"  
03.pat2 = "a.*d"  
04.pat3 = "^a.*c$"  
05.print re.match(pat1, str1)  ##None,由於沒有使用多行,因此第一行的結尾爲'\n'而不是‘2’  
06.print `re.match(pat1, str1, re.M).group()`  ###返回 'ab12',由於使用了多行,因此第一行能夠匹配出結果  
07.##對於跨行的內容進行匹配時,re.M不能生效  
08.print re.match(pat2, str1, re.M)  ##None,雖然使用了多行,可是仍匹配不成功,由於多行標識隻影響行的開頭和結尾標識,在其它匹配中不起做用。  
09.##跨行的內容進行匹配時使用,re.S,  
10.print `re.match(pat2, str1, re.S).group()`  ###返回 'ab12\nbd',使用了re.S,則‘.’能夠匹配包含'\n'在內的任意字符,因此能夠匹配成功  
11.print `re.match(pat3, str1, re.S).group()`  ###返回 'ab12\nbdc',使用了re.S,則沒有換行的概念,因此整個字符串做爲1行來匹配  
相關文章
相關標籤/搜索