Мое мнение о проблеме:
import re
def match(s, m):
m = re.match("(.*?)?((?:" + m + "){2,})(.*?)?$", s)
return (m.groups()[0], m.groups()[1], m.groups()[2]) if m else (None, None, None)
print(match("abcdbcdbcde", "bcd"))
print(match("bcdbcdbcd", "bcd"))
print(match("abcdbcdbcd", "bcd"))
print(match("bcdbcdbcde", "bcd"))
print(match("axxbcdbcdxxe", "bcd"))
print(match("axxbcdxxe", "bcd")) # only one bcd in the middle
Печать:
('a', 'bcdbcdbcd', 'e')
('', 'bcdbcdbcd', '')
('a', 'bcdbcdbcd', '')
('', 'bcdbcdbcd', 'e')
('axx', 'bcdbcd', 'xxe')
(None, None, None)