在前面学习了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. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)

    ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061) 报错原因:电脑之前有个5.0.2版本的mys ...

  2. AttributeError: module 'openai' has no attribute 'ChatCompletion'的解决办法

    原因 openai库版本过旧 解决办法(二选一) pip install -U openai 下载安装包放入你的项目根目录下, 改名格式zip为whl(即:openai-0.27.0-py3-none ...

  3. 前端下载图片、pdf、excel、world文件;前端下载图片和pdf文件;前端a标签下载图片和pdf文件;下载文件名称不生效原因

    https://blog.csdn.net/i_am_a_div/article/details/125050379 https://blog.csdn.net/moguzhale/article/d ...

  4. [Python灰帽子-黑客与逆向工程师的Python编程之道]书籍

    [Python灰帽子-黑客与逆向工程师的Python编程之道]PDF高清版免费下载地址 提取码:76aw 内容简介  · · · · · · <Python灰帽子>是由知名安全机构Immu ...

  5. TensorFlow中的Session

    这一次我们会讲到 Tensorflow 中的 Session, Session 是 Tensorflow 为了控制,和输出文件的执行的语句. 运行 session.run() 可以获得你要得知的运算结 ...

  6. Idea2020.2.3 创建JavaWeb项目(部署Tomcat)方法

    1.创建项目不再是Java Enterprise了,而是先New 一个普通Java项目! 2.创建项目后,选择Run->Edit Configuration->左上角加号->Tomc ...

  7. 宝塔Linux定时shell

    定时清除缓存文件 rm -rfv /www/wwwroot/www.xxx.com/runtime 定时解压,常用于定时恢复站点 解压到当前 cd /www/wwwroot/www.xxx.com t ...

  8. 八、常用Api

    Object 深拷贝和浅拷贝 Objects 包装类 StringBuilder StringJoin Math System RuntimeBigDecimal Date SImpleDateFor ...

  9. 1.markdown

    markdown 使用学习 # +标题名字 选择标题 最多6级 一个#表示加一级 一级最大 字体 对字体加粗等操作 hello **hello** 粗体 hello *hello* 斜体 hello ...

  10. SimplCommerce 核心

    EF配置 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks ...