如何在 Python 正则表达式中使用 re.finditer() 方法?
据 Python 文档所示,
re.finditer(pattern, string, flags=0)
返回一个迭代器,在字符串中 yielded 所有非重叠的 RE 模式的 MatchObject 的实例。从左到右扫描字符串,按找到的顺序返回匹配项。空匹配也包括在结果中。
以下代码展示了 Python 正则表达式 中 re.finditer() 方法的用法
示例
import re s1 = 'Blue Berries' pattern = 'Blue Berries' for match in re.finditer(pattern, s1): s = match.start() e = match.end() print 'String match "%s" at %d:%d' % (s1[s:e], s, e)
输出
Strings match "Blue Berries" at 0:12
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP