在前面学习了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. TiDB日常运维手册

    1.处理dm不兼容DDL 迁移中断,执行跳过操作 tiup dm list tiup dm display xxx-dm-prod tiup dmctl --master-addr 10.0.xx.x ...

  2. 周练3(php反序列化)

    serialize()函数 $s = serialize($变量); //该函数将变量数据进行序列化转换为字符串 file_put_contents('./目标文本文件', $s); //将$s保存到 ...

  3. 初识redis之性能测试

    最近接触一项新技术的方法都是通过测试来入门的.对测试这件事情有了新的认识,觉得是类似做实验的一种方式.尤其对于后端,测试的性能指标是技术选型的重要参考. 好了,如果你想做一下redis的性能测试,不要 ...

  4. (pymssql._pymssql.OperationalError) (8152, b'String or binary data would be truncated.DB-Lib error message 20 018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n')

    (pymssql._pymssql.OperationalError) (8152, b'String or binary data would be truncated.DB-Lib error m ...

  5. golang for 循环

    1.for 循环 for循环是Golang唯一的循环语句. for 初始表达式; 布尔表达式; 迭代因子 { 循环体; } package main import "fmt" fu ...

  6. RabbitMQ的安装(linux版)

    原文地址:https://blog.csdn.net/jiguquan3839/article/details/91346261 注意:在web管理页面登录提示"User can only ...

  7. JavaSE——复杂对象数组练习

    定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同. 学生的属性:学号,姓名,年龄. 要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断. 要求2 ...

  8. PHP安装SOAP扩展调用webservice获取数据

    报错内容: 调用方式: 错误原因: URL未加后缀?WSDL导致异常,加入后异常问题解决.

  9. HIVE- INSERT 方法使用

    (1) INSERT INTOINSERT INTO tableVALUES ('aaa' , 111),('bbb' , 222); (2) insert overwrite insert over ...

  10. 基于Geojson的点集的抽稀Js实现

    由于要进行反距离插值,离散点太多肯定会影响插值的效率. 为了提升插值速度,就有了这个点的抽稀. 参考这位仁兄的思路.http://blog.csdn.net/cdl2008sky/article/de ...