正则对象的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只能显示有分组名的数据

最新文章

  1. 【优雅代码】深入浅出 妙用Javascript中apply、call、bind
  2. bs4_3select()
  3. 使用text存储hash类型的数据 Use text filed to store the hash map
  4. 如何实现Qlikview的增量数据加载
  5. hiho1123_好配对
  6. UVa 11542 (高斯消元 异或方程组) Square
  7. 覆盖equals的时候总要覆盖hashCode
  8. SE 2014年4月24日
  9. Gitlab,Github与Bitbucket
  10. HOW TO LINK THE TRANSACTION_SOURCE_ID TO TRANSACTION_SOURCE_TYPE_ID
  11. TabBarController和其他view无法建立Relationship segue的原因
  12. 简化异常处理的Throwables类
  13. Java与C/C++有什么区别?
  14. Linux的基础命令
  15. 创建http.Server实例,端口占用就换个端口
  16. node 和npm 版本更新
  17. 巧妙解决windows下 copy命令不接受太长路径的问题
  18. springboot获取application.yml中的配置信息
  19. python oracle使用心得
  20. 【Java并发编程】:并发新特性—障碍器CyclicBarrier

热门文章

  1. Eclipse 隐藏已关闭的项目
  2. C# : 泛型的继承关系实现的一个可以存放不同数据类型的链表
  3. Oracle DB , 计算各个用户/schema 的磁盘占用空间
  4. tomcat ----&gt; 源码关联/编译/....
  5. LeetCode--429--N叉树的层序遍历
  6. 什么是EOS(不一样的角度看柚子)
  7. android -------- ConstraintLayout 宽高比和偏移量比(三)
  8. confirm提示弹出确定和取消按钮
  9. 『OpenCV3』Mat简介
  10. bzoj3884: 上帝与集合的正确用法 扩展欧拉定理