import re
regex = re.compile(r"^(?!(?:(?!g1|g2).)*(?:g21|g22)(?:(?!g31|g32|g41|g42).)*(?:g11|g12)).*?(?:g11|g12).*$", flags=re.MULTILINE)
test_str = ("'g11'\n"
"'g31 g11'\n"
"'g41g11'\n"
"'g11 g21 g11'\n"
"'anything or nothing g11 anything or nothing'\n"
"'anything or nothing g31 anything or nothing g11'\n"
"'anything'\n"
"''\n\n"
"'g31 g21 g11'\n"
"'g21 g11 g31'\n\n\n")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} 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