import re
regex = re.compile(r"^(?!.*printf)([^\"\n]*\"[^\"\n]*\")*?[^\"\n]*\"[^\"\n]{5,}\"", flags=re.MULTILINE)
test_str = ("\"Match\":\n"
"write(\"blueberry\"); // yum\n"
"y = 34; write(\"banana\")\n"
"z = \"pineapple\";\n\n\n\n"
"Not match:\n"
"p = \"seed\";\n"
"printf(\"-%s-\", \"strawberry\"); // whatever\n"
"x = 12; printf(\"lime\"); write(\"coconut\")\n"
"x = 12;\"\"\"\" write(\"coconut\"); printf(\"lime\");\n"
"write(\"lime\"); write(\"lime\");\n"
"write(\"lime\"); write(\"abc printf abc\"); write(\"lime\");\n"
"write(\"lime\"); write(\"abc printf\"); write(\"lime\");\n"
"write(\"lime\"); write(\"p printf\"); write(\"lime\");\n"
"write(\"lime\"); write(\"pineapple printf\"); write(\"lime\");\n"
"write(\"lime\"); write(\" printf\"); write(\"lime\");\n"
"write(\"lime\"); write(\"printf abc\"); write(\"lime\");\n"
"write(\"lime\"); write(\"printf\"); write(\"lime\");\n"
"write(printf\"blueberry\"); // yum\n"
"write(\"blueberry\"printf); // yum\n"
"write(\"blueberry\"); printf // yum\n"
"z = \"pineapple printf\";")
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