import re
regex = re.compile(r"^ZZ=.*\r?\n.*\r?\n(?!XXX\b).*(?:\r?\n.+)*(?:\r?\n){2}.*\r?\n.*\s*", flags=re.MULTILINE)
test_str = ("ZZ=101\n"
"OO\n"
"XXX\n"
"111111111111\n"
"222222222222\n\n"
"00000000000\n"
"AAAAAAAAAAAA\n\n\n"
"valid file example\n\n\n"
"ZZ=101\n"
"OO\n"
"XXX\n"
"111111111111\n"
"222222222222\n\n"
"00000000000\n"
"AAAAAAAAAAAA\n\n\n"
"Not valid file\n\n\n"
"ZZ=101\n"
"OO\n"
"XSS (should be rejected except XXX)\n"
"111111111111\n"
"222222222222\n\n"
"00000000000\n"
"AAAAAAAAAAAA\n\n\n"
"Not valid file\n\n\n"
"HH=101 (should be rejected except ZZ=)\n"
"OO\n"
"XXX (should be rejected except XXX)\n"
"111111111111\n"
"222222222222\n\n"
"00000000000\n"
"AAAAAAAAAAAA")
match = regex.search(test_str)
if match:
print(f"Match was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html