Python 正则表达式re模块( 二 )


span([group]) 方法返回 (start(group), end(group)) 。
再看看一个例子:
>>> import re>>> pattern = re.compile(r'([a-z]+) ([a-z]+)', re.I) # re.I 表示忽略大小写>>> m = pattern.match('Hello World Wide Web')>>> print (m) # 匹配成功,返回一个 Match 对象<_sre.SRE_Match object at 0x10bea83e8>>>> m.group(0) # 返回匹配成功的整个子串'Hello World'>>> m.span(0) # 返回匹配成功的整个子串的索引(0, 11)>>> m.group(1) # 返回第一个分组匹配成功的子串'Hello'>>> m.span(1) # 返回第一个分组匹配成功的子串的索引(0, 5)>>> m.group(2) # 返回第二个分组匹配成功的子串'World'>>> m.span(2) # 返回第二个分组匹配成功的子串(6, 11)>>> m.groups() # 等价于 (m.group(1), m.group(2), ...)('Hello', 'World')>>> m.group(3) # 不存在第三个分组Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: no such groupsearch 方法
search 方法用于查找字符串的任何位置,它也是一次匹配,只要找到了一个匹配的结果就返回,而不是查找所有匹配的结果,它的一般使用形式如下:
search(string[, pos[, endpos]])其中,string 是待匹配的字符串,pos 和 endpos 是可选参数,指定字符串的起始和终点位置,默认值分别是 0 和 len (字符串长度) 。
当匹配成功时,返回一个 Match 对象,如果没有匹配上,则返回 None 。
让我们看看例子:
>>> import re>>> pattern = re.compile('d+')>>> m = pattern.search('one12twothree34four') # 这里如果使用 match 方法则不匹配>>> m<_sre.SRE_Match object at 0x10cc03ac0>>>> m.group()'12'>>> m = pattern.search('one12twothree34four', 10, 30) # 指定字符串区间>>> m<_sre.SRE_Match object at 0x10cc03b28>>>> m.group()'34'>>> m.span()(13, 15)再来看一个例子:
# -*- coding: utf-8 -*-import re# 将正则表达式编译成 Pattern 对象pattern = re.compile(r'd+')# 使用 search() 查找匹配的子串,不存在匹配的子串时将返回 None# 这里使用 match() 无法成功匹配m = pattern.search('hello 123456 789')if m: # 使用 Match 获得分组信息 print ('matching string:',m.group()) # 起始位置和结束位置 print ('position:',m.span())执行结果:
matching string: 123456position: (6, 12)findall 方法
上面的 match 和 search 方法都是一次匹配,只要找到了一个匹配的结果就返回 。然而,在大多数时候,我们需要搜索整个字符串,获得所有匹配的结果 。
findall 方法的使用形式如下:
findall(string[, pos[, endpos]])
其中,string 是待匹配的字符串,pos 和 endpos 是可选参数,指定字符串的起始和终点位置,默认值分别是 0 和 len (字符串长度) 。
findall 以列表形式返回全部能匹配的子串,如果没有匹配,则返回一个空列表 。
看看例子:
import repattern = re.compile(r'd+') # 查找数字result1 = pattern.findall('hello 123456 789')result2 = pattern.findall('one1two2three3four4', 0, 10)print (result1)print (result2)执行结果:
['123456', '789']['1', '2']再先看一个栗子:
# re_test.pyimport re#re模块提供一个方法叫compile模块,提供我们输入一个匹配的规则#然后返回一个pattern实例,我们根据这个规则去匹配字符串pattern = re.compile(r'd+.d*')#通过partten.findall()方法就能够全部匹配到我们得到的字符串result = pattern.findall("123.141593, 'bigcat', 232312, 3.15")#findall 以 列表形式 返回全部能匹配的子串给resultfor item in result: print (item)运行结果:
123.1415933.15finditer 方法
finditer 方法的行为跟 findall 的行为类似,也是搜索整个字符串,获得所有匹配的结果 。但它返回一个顺序访问每一个匹配结果(Match 对象)的迭代器 。
看看例子:
# -*- coding: utf-8 -*-import repattern = re.compile(r'd+')result_iter1 = pattern.finditer('hello 123456 789')result_iter2 = pattern.finditer('one1two2three3four4', 0, 10)print (type(result_iter1))print (type(result_iter2))print 'result1...'for m1 in result_iter1: # m1 是 Match 对象 print ('matching string: {}, position: {}'.format(m1.group(), m1.span()))print 'result2...'for m2 in result_iter2: print ('matching string: {}, position: {}'.format(m2.group(), m2.span()))执行结果:
<type 'callable-iterator'><type 'callable-iterator'>result1...matching string: 123456, position: (6, 12)matching string: 789, position: (13, 16)result2...matching string: 1, position: (3, 4)matching string: 2, position: (7, 8)


推荐阅读