import re START_ITEM = "< ITEM >" END_ITEM = "< END ITEM >" TEXT = "1234567890" _ITEM_RE = re.compile(''' <\s*ITEM\s*> # Start \s*(.*?)\s* # Body (group 1) <\s*END\s+ITEM\s*> # End ''' % locals(), re.VERBOSE | re.DOTALL) string = '%s%s%s%s' % (START_ITEM, 200*TEXT, END_ITEM, 1*TEXT) # Find text inside item and after it res = _ITEM_RE.search(string) inside_the_match = res.group(1) after_match = string[res.end():] actual = (inside_the_match, after_match) expected = (200*TEXT, 1*TEXT) assert actual == expected, "%s\n!=\n%s" % (actual, expected)