merged = []
resultindex = self._resultindex()
it = util.peekable(iter(matches))
curr = it.next()
while it.hasnext():
next = it.peek()
// we have to make sure that there"s no matchresult from another group
// between curr and next
if curr.text != next.text or (resultindex[curr] != resultindex[next] - 1):
merged.append(curr)
curr = it.next()
else:
logger.debug("merging adjacent identical matches %d and %d", it.index - 1, it.index)
del resultindex[curr]
newindex = resultindex[next]
del resultindex[next]
it.next()
curr = matchresult(curr.start, next.end, curr.text, curr.match)
resultindex[curr] = newindex
merged.append(curr)
return merged