Skip to content

Instantly share code, notes, and snippets.

@goern
Created June 27, 2018 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goern/c06a82021ba68ac759262ef2a5950bad to your computer and use it in GitHub Desktop.
Save goern/c06a82021ba68ac759262ef2a5950bad to your computer and use it in GitHub Desktop.
regexp for a semver compliant pre-release
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9alpha|beta|rc.-]+))?(?:\+([0-9a-zA-Z.-]+))?"
test_str = ("0.0.1\n"
"0.1.0-rc.3\n"
"0.1.0-alpha.12\n"
"1.1.1\n"
"0.5.0-beta.3+dfashfjlkjhflksah\n"
"0.5.0-alpha\n")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment