import re
regex = re.compile((r"(<othermeta content=\"V6\" name=\"version\"\/>)(?![\s\S]*<othermeta content=\"V7\" name=\"version\"\/>)\n"), flags=re.MULTILINE)
test_str = ("I will search for V6\n\n"
"If i find V6, i'll check for V7 AFTER V7\n\n"
"If V7 does not exist i will match V6 and replace it\n"
"If V7 exists AFTER V6, i will NOT match V6\n"
"BUG: If V7 exists BEFORE V6, i will match V6 (i don't know how to create a REGEX that looks ahead and behind)\n\n"
"Test it by changing Vy below to V7 and the match will be gone.\n"
"BUG: If you change Vx to V7 it will still match V6. \n\n"
"<othermeta content=\"Vx\" name=\"version\"/>\n"
"<othermeta content=\"V5\" name=\"version\"/>\n"
"<othermeta content=\"V6\" name=\"version\"/>\n"
"<othermeta content=\"V6.1\" name=\"version\"/>\n"
"<othermeta content=\"V\" name=\"version\"/>")
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