Regular Expression For Version Number Bigger Than 1.18.10
Solution 1:
Don't use regular expressions for this. Use split
and tuple
comparison:
defis_recent(version):
version_as_ints = (int(x) for x in version.split('.'))
returntuple(version_as_ints) > (1, 18, 10)
And then check is_recent("1.10.11")
or is_recent("1.18.12")
Solution 2:
Seems like this battery has already been included in Python in distutils.version
:
from distutils.version import LooseVersion
LooseVersion("1.18.11") > LooseVersion("1.18.10")
#True
LooseVersion("1.2.11") > LooseVersion("1.18.10")
#False (note that "2">"18"isTrue)
LooseVersion("1.18.10a") > LooseVersion("1.18.10")
#True
This takes into account splitting and comparing both version number parts as integers, and non-numeric parts (e.g alphabetic extension) seperately and correctly. (If you want the alternate behaviour, (lexicographical comparison), you can directly compare the tuples of strings that result on a version_num.split(".")
)
Note that there is also a StrictVersion
variant that will throw an exception (ValueError
) on alphabetic characters in the version string. See also PEP386 which is planning to deprecate both, replacing them with a NormalizedVersion
.
Solution 3:
Don't use regular expression for that, but something like:
major, minor, patch = v.split('.')
ifint(major) > 1or (int(major) == 1andint(minor) >= 18):
...
Solution 4:
Not sure exactly why you need a regex, it's not a particularly good tool for doing complex range checking.
I would just split
the string into a three-element array and then check each element, something like:
(p1, p2, p3) = verstr.split(".")
# May want to check they're numeric first.ifint(p1) < 1: returnFalseifint(p1) == 1:
ifint(p2) < 18: returnFalseifint(p2) == 18:
ifint(p3) < 10: returnFalsereturnTrue
Post a Comment for "Regular Expression For Version Number Bigger Than 1.18.10"