正则对象的findall方法

 

findall(string[, pos[, endpos]]) 

搜索string,以列表形式返回全部能匹配的子串.

import re
p1 = re.compile(r'\d+')
a_str = 'one1two222three33four4'
#正则对象的split方法,使用正则匹配进行分割字符串
#最后是以列表的形式返回
print(p1.split(a_str))

#正则对象的findall方法,来查找符合对象的子字符串
#最后是以列表的形式返回
print(p1.findall(a_str))

#finditer 每个返回值都是是一个对象,用group()方法查看,
for i in p1.finditer(a_str):
    print(i.group())

输出结果:

['one', 'two', 'three', 'four', '']
['1', '222', '33', '4']
1
222
33
4

 

sub方法

sub(repl, string[, count])

使用repl替换string中每一个匹配的子串后返回替换后的字符串。

当repl是一个字符串时,可以使用\id或\g<id>、\g<name>引用分组,但不能使用编号0。

当repl是一个方法时,这个方法应当只接受一个参数(Match对象),并返回一个字符串用于替换(返回的字符串中不能再引用分组)。

count用于指定最多替换次数,不指定时全部替换。

import re
p = re.compile(r'(\w+) (\w+)')
s = 'i say,hello word!'
print(p.sub(r'\2 \1',s))

def func(m):
    return m.group(1).title() + ' ' +m.group(2).title()
print(p.sub(func,s))

输出结果:
say i,word hello!
I Say,Hello Word!

#解释:
#\(id)就是匹配的括号的内容,id从默认从1开始计数
#m.group(1)是一个字符串,调用字符串的title()方法,所有单词的搜字母大写。

 

  match匹配对象

Match对象是一次匹配的结果,包含了很多关于此次匹配的信息,可以使用Match提供的可读属性或方法来获取这些信息。上面的过程中多次使用了match对象,调用了他的group()和groups()等方法。

import re
prog = re.compile(r'(?P<tagname>abc)(\w*)(?P=tagname)')
result = prog.match('abclfjlad234sjldabc')

# finiter 迭代以后每个对象都是一个matche对象

print(dir(result)) #查看match方法
print(result.group()) #group方法
print(result.groups())
print(result.group(2))
print(result.group(1))
print('####'*10 + 'tagname' + '####'*10)
print(result.group('tagname'))
#matche对象的group返回一个元组,下标是以1开头

print(result.groupdict())

输出结果:

['__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']
abclfjlad234sjldabc
('abc', 'lfjlad234sjld')
lfjlad234sjld
abc
########################################tagname########################################
abc
{'tagname': 'abc'}

解释:

1,  我们可以看到result已经由字符串转换成了一个正则对象。

2,  resule.groups()可以查看出来所有匹配到的数据,每个()是一个元素,最终返回一个tuple

3,  group()既可以通过下标(从1开始)的方式访问,也可以通过分组名进行访问。

4,  groupdict只能显示有分组名的数据

 

posted on 2017-11-14 21:35  菜菜的痛  阅读(115)  评论(0编辑  收藏  举报