在前面学习了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. python获取当前季度或上一季度的起止日期

    import datetime import calendar def get_quarter_date(quarter='current'): """ 获取当前季度或上 ...

  2. allure+junit5遇到的一些问题

    java+junit5+allure 之前引testng,还比较顺利,见上一篇博客,然后testng的注解和junit不一样,感觉junit5更好用一些,所以尝试java+junit5+allure ...

  3. linux rpm 命令

    # 产看软件包信息[root@devops201 k8s_install]# rpm -qi yum-utilsName : yum-utilsVersion : 1.1.31Release : 54 ...

  4. COMP3357 Cryptography

    课程内容笔记,自用,不涉及任何 assignment,exam 答案 Notes for self use, not included any assignments or exams Course ...

  5. 提高NTC测温精度(转发)

    (一)一般精度要求:采样数据的获取,直接采用恒流源(或恒压源)上拉方式.见图(2)所示.  原理:将恒流源(或恒压源)直接作用于NTC热敏电阻Rt上,当被测对象的温度发生变化,NTC热敏电阻的阻值Rt ...

  6. Mysql之迂回连接术

    转载请注明来源:https://www.cnblogs.com/Sherlock-L/p/14932870.html 关键词:OmniDB.Mysql Router 背景:项目的测试数据库放在了生产机 ...

  7. debian 新安装备忘

    change resource https://www.cnblogs.com/devzyh/p/12670194.html 安装sougou输入法,并配置好.重启. https://www.cnbl ...

  8. android audiorecord初始化失败相关资料收集

    We're also struggling with audio recording on some Samsung Android Devices. Unfortunately it seems t ...

  9. firefox 利用 Selenium IDE 对 DBackup 进行自动化测试

    今天看<编写可维护的JavaScript>的时候有一章是专门讲 Selenium 对 JavaScript 进行自动化测试的. 在了解了 Selenium 的强大之后,动手试验了一下 fi ...

  10. Python学习笔记(二)变量的使用

    一.变量的定义 把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做"变量" 可以把变量看做保存信息的容器,它们的目的是在内存中标注和存储数据,然后可 ...