import re
regex = re.compile(r"Vuln : Upgrade\s*(?<vulnNameFromTo>.*?)\s+from\s+(?<vulnCurrentVersionFromTo>.*?)\s+to\s+(?<vulnFixVersionFromTo>.+?)(?:\s+for\b.*)?$", flags=re.MULTILINE)
test_str = ("Vuln : Upgrade pypi://onnx from 1.12.0 to 1.13.0 Final\n"
"Vuln : Upgrade pypi://onnx from 1.12.0 to 1.13.0 Final for github.com/blah/blah\n\n"
"I specifically just need to grab the strings pypi://onnx 1.12.0 1.13.0 Final.")
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