import re
regex = re.compile(r"""
^(main|development|master|(features|tests|(bug|hot)fix)(\/[a-zA-Z0-9]+([-_][a-zA-Z0-9]+)*){1,2}|release\/[0-9]+(\.[0-9]+)*(-(alpha|beta|rc)[0-9]*)?)$
""", flags=re.MULTILINE | re.VERBOSE)
test_str = ("development\n"
"main\n"
"bugfix/user-list\n"
"bugfix/user_list\n"
"bugfix/123\n"
"bugfix/123/account-update\n"
"bugfix/123/account_update\n"
"bugfix/User_crud/account-update\n"
"bugfix/User_crud/account_update\n"
"tests/api\n"
"tests/123\n"
"tests/123/hello\n"
"release/1.0.1\n"
"release/1.0.1-beta1\n"
"release/1.0.1-beta\n"
"release/1.0.1-rc3\n\n\n"
"it should not match following\n"
"bugfix/\n"
"bugfix/-\n"
"bugfix/_\n"
"bugfix/-name\n"
"bugfix/_name\n"
"bugfix/-/name\n"
"bugfix/-/-\n"
"bugfix/-/_\n"
"release/v1.0.1")
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