import re
regex = re.compile(r"^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<preRelease>(?:[a-zA-Z1-9][a-zA-Z\d]*|0\d*[a-zA-Z][a-zA-Z\d]*|0)(?:\.(?:[a-zA-Z1-9][a-zA-Z\d]*|0\d*[a-zA-Z][a-zA-Z\d]*|0))*))?(?:\+(?<metadata>(?:[a-zA-Z\d-]*)(?:\.(?:[a-zA-Z\d-]*))*))?$", flags=re.MULTILINE)
test_str = ("0.1.0\n"
"01.2.1\n"
"1.2.0\n"
"1.0.0\n"
"1.01.0\n"
"1.0.01\n"
"1.0.0-0df34.ahgdfhsdf.0005a56tg\n"
"1.0.0-0054.df4gf\n"
"1.0.0-005a4.df4gf\n"
"1.0.0-005a4.df4gf+adffbff.sdfg.sdfg.adf78df7d8df7\n"
"1.0.0-005a4.df4gf+adffbff.sdfg.sdfg.00000000.dfsg.sdf-sfg.sdfg-sdfg.sgf\n"
"900.10.85494\n"
"0900.10.54")
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