25 爬虫 - re 正则 finditer 方法
2023-09-11 14:15:43 时间
finditer 方法的行为跟 findall 的行为类似,也是搜索整个字符串,获得所有匹配的结果。但它返回一个顺序访问每一个匹配结果(Match 对象)的迭代器。
看看例子:
# -*- coding: utf-8 -*-
import re
pattern = re.compile(r'\d+')
result_iter1 = pattern.finditer('hello 123456 789')
result_iter2 = pattern.finditer('one1two2three3four4', 0, 10)
print type(result_iter1)
print type(result_iter2)
print 'result1...'
for m1 in result_iter1: # m1 是 Match 对象
print 'matching string: {}, position: {}'.format(m1.group(), m1.span())
print 'result2...'
for m2 in result_iter2:
print 'matching string: {}, position: {}'.format(m2.group(), m2.span())
执行结果:
<type 'callable-iterator'>
<type 'callable-iterator'>
result1...
matching string: 123456, position: (6, 12)
matching string: 789, position: (13, 16)
result2...
matching string: 1, position: (3, 4)
matching string: 2, position: (7, 8)
相关文章
- PC-删除共享[绝对够狠的方法]
- SQL2008-删除时间字段重复的方法
- 【Python成长之路】Python爬虫 --requests库爬取网站乱码(xe4xb8xb0xe5xa)的解决方法
- JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
- Flash完美跨域访问的方法
- 数据仓库建模方法初步
- java中eclipse控制台接受输入的方法
- anaconda的安装教程和使用方法
- SAP CDS view(Core Data Service)自学的一些材料和方法
- Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理
- Python之 sklearn:sklearn中的RobustScaler 函数的简介及使用方法之详细攻略
- 基于CNN-GRU-Attention混合神经网络的负荷预测方法(Python代码实现)
- js获取文件后缀名方法
- mysql中文乱码解决方法
- 软件测试技术之性能测试进阶—并发测试的方法
- Python爬虫入门之爬虫解析提取数据的四种方法
- 爬虫===登陆CSDN的方法
- python爬虫-使用xpath方法
- 按键消抖的两种方法--中断延迟工作与定时器