'Regex to match PEP440 compliant version strings
PEP 440 lays out what is the accepted format for version strings of Python packages.
These can be simple, like: 0.0.1
Or complicated, like: 2016!1.0-alpha1.dev2
What is a suitable regex which could be used for finding and validating such strings?
Solution 1:[1]
I think this should comply with PEP440:
^(\d+!)?(\d+)(\.\d+)+([\.\-\_])?((a(lpha)?|b(eta)?|c|r(c|ev)?|pre(view)?)\d*)?(\.?(post|dev)\d*)?$
Explained
Epoch, e.g. 2016!:
(\d+!)?
Version parts (major, minor, patch, etc.):
(\d+)(\.\d+)+
Acceptable separators (., - or _):
([\.\-\_])?
Possible pre-release flags (and their normalisations; as well as post release flags r or rev), may have one or more digits following:
((a(lpha)?|b(eta)?|c|r(c|ev)?|pre(view)?)\d*)?
Post-release flags, and one or more digits:
(\.?(post|dev)\d*)?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Leo |
