您的位置:首页 > 编程语言 > Python开发

python应用regex正则表达式模块re

2011-12-13 22:38 761 查看
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re

def regex():
str = 'abcdab'
patstr = 'ab'
##可以匹配的2种方式:1
patobj = re.compile(patstr)
got = patobj.match(str)
##2
got = re.match(patstr,str)

##几个基本的匹配函数
result = re.match('ab','abcd')   #match方法基本等同于re.search('^ab','abcd'),即默认就是前置匹配,只匹配字串的开头
if result:
print result.group()
else:
print result

result = patobj.match('0abcd')
if result:
print result.group()
else:
print result, 'There is no result'

result = patobj.search('0abcd')  ##匹配模式并返回第一个匹配对象
if result:
print result.group()
else:
print result

str = 'abcdab'
result = patobj.findall(str)  ##返回一个包含所有匹配结果的列表,如果匹配字串中有分组的话,则返回由分组内容组成的元组所组成的列表
if result:                    ##即无分组时返回有多个group()结果的列表,有分组时返回有多个groups()结果的列表, 见下
print type(result), result
else:
print result

result = patobj.finditer(str)  ##返回一个包含所有匹配结果的迭代器,可以配合findall使用
if result:
print type(result), result.next().group()
else:
print result

result = patobj.sub('__','abcdab')  ##用指定的字符替换所有匹配到的字符串
if result:
print 'replace:',result    ##__cd__
else:
print result

result = patobj.subn('__','abcdab')  ##用指定的字符替换所有匹配到的字符串,还包括替换数目
if result:
print 'replace:',result   ##('__cd__', 2)
else:
print result

##基本的几个结果查询方法:
str = '[1]aaaa[2]bbbb[3]cccc@1[:]dddd'
result = re.search(r'\[(\d+\](.*)@1\[(.*))\]',str)
if result:
print result.group()   ##返回匹配到的第一个完整字符串: [1]aaaa[2]bbbb[3]cccc@1[:]
print result.group(1)  ##返回匹配的字符串中的第一个分组,就是第一个左扩弧和其对应的右扩弧中的所有包含的所有内容. 1]aaaa[2]bbbb[3]cccc@1[:
print result.group(2)  ##aaaa[2]bbbb[3]cccc
print result.group(3)  #最大为3,因为匹配字串里只有3个扩弧, :
print result.groups()  ###把所有扩弧分组的内容放在一个元组对象中,并返回:('1]aaaa[2]bbbb[3]cccc@1[:', 'aaaa[2]bbbb[3]cccc', ':')
else:
print result

##几个基本的匹配方式:贪婪与非贪婪
str = '[1]aaaa[2]bbbb[3]cccc@1[:]dddd'
result = re.search(r'\[.*\]',str)
if result:
print result.group()     ##[1]aaaa[2]bbbb[3]cccc@1[:]
else:
print result

str = '[1]aaaa[2]bbbb[3]cccc@1[:]dddd'
result = re.search(r'\[.*?\]',str)   ###用一个?来控制贪婪
if result:
print result.group()     ##[1]
else:
print result

##其它的基本匹配和所有的语言都一样,通用的,除了一些高级的用法,不过可以参考官方手册中的样例,如果有需要的话

if __name__ == '__main__':
regex()


--------------------------------------------------------补充分割线------------------------------------------------------------------------

最近用到了几个标识的参数,比如:忽略大小写,多行搜索等。这里补充一下:

有哪些标识可以使用?

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):
详细模式。这个模式下正则表达式可以是多行,忽略空白字符,并可以加入注释。

哪些函数支持这些标识?

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多行只能影响^和$匹配,不会影响跨行的内容匹配,比如:
str1 = "ab12\nbdc"
pat1 = "^a.*2$"
pat2 = "a.*d"
pat3 = "^a.*c$"
print re.match(pat1, str1)  ##None,因为没有使用多行,所以第一行的结尾为'\n'而不是‘2’
print `re.match(pat1, str1, re.M).group()`  ###返回 'ab12',因为使用了多行,所以第一行可以匹配出结果
##对于跨行的内容进行匹配时,re.M不能生效
print re.match(pat2, str1, re.M)  ##None,虽然使用了多行,但是仍匹配不成功,因为多行标识只影响行的开头和结尾标识,在其它匹配中不起作用。
##跨行的内容进行匹配时使用,re.S,
print `re.match(pat2, str1, re.S).group()`  ###返回 'ab12\nbd',使用了re.S,则‘.’可以匹配包含'\n'在内的任意字符,所以可以匹配成功
print `re.match(pat3, str1, re.S).group()`  ###返回 'ab12\nbdc',使用了re.S,则没有换行的概念,所以整个字符串作为1行来匹配
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: