且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中查找字符串中所有出现的开始和结束位置

更新时间:2021-12-05 08:54:52

可以使用正则表达式;匹配对象带有位置信息附加.使用 Python 2 的示例:

You can use regular expressions; the match objects come with position information attached. Example using Python 2:

>>> import re
>>> example = 'abcdefabcdefabcdefg'
>>> for match in re.finditer('abc', example):
        print match.start(), match.end()
0 3
6 9 
12 15