在前面学习了findall()函数,它可以一次性找到多个匹配的字符串,但是不能提供所在的位置,并且是一起返回的,如果有数万个一起返回来,就不太好处理了,因此要使用finditer()函数来实现每次只返回一个,并且返回所在的位置,如下例子:

  1. #python 3. 6
  2. #蔡军生
  3. #http://blog.csdn.net/caimouse/article/details/51749579
  4. #
  5. import re
  6. text = 'http://blogcsdn.net/caimouse abbaaabbbbaaaaa'
  7. pattern = 'ab'
  8. for match in re.finditer(pattern, text):
  9. s = match.start()
  10. e = match.end()
  11. print('Found {!r} at {:d}:{:d}'.format(
  12. text[s:e], s, e))

结果输出如下:

Found 'ab' at 29:31
Found 'ab' at 34:36

re的finditer()的更多相关文章

  1. Re.findall() & Re.finditer()的用法

    re.findall(pattern, string, flags=0) Return all non-overlapping matches of pattern in string, as a l ...

  2. python正则表达式--findall、finditer方法

    findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...

  3. Python中re的match、search、findall、finditer区别

    原文地址: http://blog.csdn.net/djskl/article/details/44357389 这四个方法是从某个字符串中寻找特定子串或判断某个字符串是否符合某个模式的常用方法. ...

  4. split与re.split/捕获分组和非捕获分组/startswith和endswith和fnmatch/finditer 笔记

    split()对字符串进行划分: >>> a = 'a b c d' >>> a.split(' ') ['a', 'b', 'c', 'd'] 复杂一些可以使用r ...

  5. Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法

    1. 使用find()方法 >>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.find( ...

  6. python 基础 8.4 re的 spilt() findall() finditer() 方法

      #/usr/bin/python #coding=utf-8 #@Time   :2017/11/18 18:24 #@Auther :liuzhenchuan #@File   :re的spli ...

  7. 【整理】python中re的match、search、findall、finditer区别

    match 从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾. search 若string中包 ...

  8. python re的findall和finditer

    记录一个现象: 今天在写程序的时候,发现finditer和findall返回的结果不同.一个为list,一个为iterator. 红色箭头的地方,用finditer写的时候,print(item.gr ...

  9. python正则表达式(5)--findall、finditer方法

    findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...

  10. re正则match、search、findall、finditer函数方法使用

    match 匹配string 开头,成功返回Match object, 失败返回None,只匹配一个. search 在string中进行搜索,成功返回Match object, 失败返回None, ...

随机推荐

  1. 【C学习笔记】day4-2 求出0~999之间的所有“水仙花数”并输出。

    2.求出0-999之间的所有"水仙花数"并输出."水仙花数"是指一个三位数,其各位数字的立方和确好等于该数本身,如:153=1+5+3?,则153是一个&quo ...

  2. SAP HANA:XS Job

    5.SAP HANA XS Job XS Job:定义重复执行的后台定期任务: 例如定时执行数据更新到Table的操作,需要使用到XS Job; SAP HANA XS Setting up Sche ...

  3. vue双向数据绑定原理简单实现

    vue双向数据绑定原理实现 准备工作 ​ 新建一个index.js文件, 一个index.html文件 ​ index.js文件中, 定义Vue类, 并将Vue并称全局变量 window.Vue = ...

  4. 10. watch的实现原理

    watch的实现原理 watch和computed一样, 也是基于 Watcher 的 组件内部使用的watch 和 外部使用的 vm.$watch()都是调用的Vue.prototype.$watc ...

  5. redis之单线程

    一.redis为何是单线程 官方给出的答案: 因为 Redis 是基于内存的操作,CPU 不会成为 Redis 的瓶颈,而最有可能是机器内存的大小或者网络带宽.既然单线程容易实现,而且 CPU 不会成 ...

  6. 收缩SqlServer数据库日志语句

    收缩SqlServer数据库日志SQL语句 USE[master] ALTER DATABASE XXX SET RECOVERY SIMPLE WITH NO_WAIT ALTER DATABASE ...

  7. ES6 新语法新特性总结中...

    1. 感觉 for of   通杀 for循环 和 for in呢 aaa 可以是array /object for (let k of aaa) {     console.log(k) }   2 ...

  8. 「postOI」Lost Array

    题意 有一个序列 \(A=\{a_1, a_2, ..., a_n\}\),按如下方式构造一个 \((n + 1) \times (n + 1)\) 的矩阵 \(B\): \(B_{i0}=0\)(\ ...

  9. vue3 门户网站搭建2-ngnix

    路由配好了,需要调试下 build 后是否可用,这时我们需要一个服务端来运行静态网页. 官网直接下载一个 windows 版本的 ngnix 即可: http://nginx.org/en/downl ...

  10. nodeJs 写个爬虫小玩意

    内容 起一个服务,爬某个网站的数据(我这里爬了个夕阳红游戏交易网站的数据),页面看到我要爬的内容 代码 1 //引入内置的http包 2 var http = require('http'); 3 c ...