在前面学习了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. How to present a paper 怎么讲好一篇文献

    Author : 如果在冬夜一个旅人 Date : 2022/05/24 目录 背景说明 1 读文献 1.1 读文献的层次 1.2 论文阅读的首轮次序 2 讲文献 2.1 The Problem to ...

  2. ifconfig查询的时候,只有lo网卡,没有ens33网卡

    问题ifconfig查询的时候,只有lo网卡,但是ifconfig -a查询的时候却有ens33网卡ls /etc/syconfig/network-scripts/查找所有网卡配置信息文件的时候,没 ...

  3. 泛微OA技巧随记

    隐藏明细表的加号按钮,如果不想让用户手工添行,必须通过自动联动添明细行,可以将明细表的加号按钮隐藏. document.getElementById('$addbutton0$').style.dis ...

  4. 利用for循环同步执行异步方法

    //定义一个异步函数 const foo1 = (i) => { return new Promise((resolve, reject) => { setTimeout(() => ...

  5. CISCO--配置单臂路由+DHCP

    CISCO--配置单臂路由+DHCP 1.在交换机中创建vlan10和20. Switch(config)#vlan 10 Switch(config-vlan)#vlan 20 2.接口Fa0/1配 ...

  6. JavaSE——String

    String类概述 String 类代表字符串,Java 程序中的所有字符串文字(例如"abc")都被实现为此类的实例.也就是说,Java 程序中所有的双引号字符串,都是 Stri ...

  7. vim学习小结

    参考书籍<Linux 从入门到精通>第二版(刘忆智 等编著) Vim编辑器 背景:Vim的设计哲学就是让使用者能够在主键盘区完成所有工作. vim是vi的增强版本,vim分为插入和命令两种 ...

  8. requests学习笔记02

    一.会话对象 会话对象让你能够跨请求保持某些参数.它也会在同一个 Session 实例发出的所有请求之间保持 cookie, 期间使用 urllib3 的 connection pooling 功能. ...

  9. SSLCipherCheck_v1.4.2

    Usage: sslciphercheck.exe -h <Host> -p <Port> -u <URL> -c <CSV File> -i < ...

  10. 数据驱动之 python + requests + Excel

    数据驱动 是根据数据来测试的,如读取 excel表中的测试用例自动填写测试结果,发送测试报告 包括以下模块: 1.获取用例 2.调用接口 3.校验结果 4.发送测试报告 5.异常处理 6.日志模块 1 ...