>>> import re
>>> groups = (((x.start(), x.end()), x.group()) for x in matches)
>>> matches = re.finditer("[^B]+", "AAABAABAAABIAABIAABAA")
>>> groups = (((x.start(), x.end()), x.group()) for x in matches)
>>> ["{}{}".format(*arg) for arg in groups]
['(0, 3)AAA', '(4, 6)AA', '(7, 10)AAA', '(11, 14)IAA', '(15, 18)IAA', '(19, 21)AA']
编辑:
正如 segfalt 指出的那样,我没有正确阅读这个问题......
>>> s = 0
>>> res = []
>>> for x in "AAABAABAAABIAABIAABAA".split('B'):
... res.append("{}{}".format((s, s+len(x)), x))
... s += len(x)
...
>>> print "".join(res)
(0, 3)AAA(3, 5)AA(5, 8)AAA(8, 11)IAA(11, 14)IAA(14, 16)AA
或者
>>> print "\n".join(res)
(0, 3)AAA
(3, 5)AA
(5, 8)AAA
(8, 11)IAA
(11, 14)IAA
(14, 16)AA