import re

pattern = r'(?:AB)+A'
s = 'qqqqABqqqqqqABAqqqqqqABABABABAqqq'
print(max(re.findall(pattern, s), key=len))