import re
p = re.compile(r'(AA)(.*?)(ZZ)|(BB)(.*?)(YY)')
test_str = "AA Text 1 here ZZ and BB Text2 there YY"
print("Contents:")
print([x.group(2).strip() for x in p.finditer(test_str) if x.group(2)])
print([x.group(5).strip() for x in p.finditer(test_str) if x.group(5)])
print("Delimiters:")
print([(x.group(1), x.group(3)) for x in p.finditer(test_str) if x.group(1) and x.group(3)])
print([(x.group(4), x.group(6)) for x in p.finditer(test_str) if x.group(4) and x.group(6)])
aW1wb3J0IHJlCnAgPSByZS5jb21waWxlKHInKEFBKSguKj8pKFpaKXwoQkIpKC4qPykoWVkpJykKdGVzdF9zdHIgPSAiQUEgVGV4dCAxIGhlcmUgWlogYW5kIEJCIFRleHQyIHRoZXJlIFlZIgpwcmludCgiQ29udGVudHM6IikgCnByaW50KFt4Lmdyb3VwKDIpLnN0cmlwKCkgZm9yIHggaW4gcC5maW5kaXRlcih0ZXN0X3N0cikgaWYgeC5ncm91cCgyKV0pCnByaW50KFt4Lmdyb3VwKDUpLnN0cmlwKCkgZm9yIHggaW4gcC5maW5kaXRlcih0ZXN0X3N0cikgaWYgeC5ncm91cCg1KV0pCnByaW50KCJEZWxpbWl0ZXJzOiIpCnByaW50KFsoeC5ncm91cCgxKSwgeC5ncm91cCgzKSkgZm9yIHggaW4gcC5maW5kaXRlcih0ZXN0X3N0cikgaWYgeC5ncm91cCgxKSBhbmQgeC5ncm91cCgzKV0pCnByaW50KFsoeC5ncm91cCg0KSwgeC5ncm91cCg2KSkgZm9yIHggaW4gcC5maW5kaXRlcih0ZXN0X3N0cikgaWYgeC5ncm91cCg0KSBhbmQgeC5ncm91cCg2KV0p