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行来匹配  
相关文章
相关标签/搜索