Regex for parsing version number

前端 未结 4 1639
失恋的感觉
失恋的感觉 2021-01-29 06:37

How can I write a regex for parsing version numbers. I want to match numbers like: 1.000, 1.0.00, 1.0.0.000 but not integers 1

4条回答
  •  时光说笑
    2021-01-29 06:48

    This doesn't directly answer your question, but it is making an assumption that you want this functionality to compare different versions of an application.

    You can do this using distutils.version (the docs are empty, but we can find function documentation in the source )

    A couple examples to help understand what we are doing:

    >>> from distutils.version import LooseVersion, StrictVersion
    >>> LooseVersion("1.0.1") < LooseVersion("1.0.2")
    True
    >>> StrictVersion("1.0.1") < StrictVersion("1.0.2")
    True
    >>> LooseVersion("1.0.10") < LooseVersion("1.0.1")
    False
    

    What's the difference between LooseVersion and StrictVersion? With LooseVersion you can compare "real world" versions like this:

    >>> LooseVersion("2.1-rc1") < LooseVersion("2.2")
    True
    

    Whereas StrictVersion doesn't allow alphabetic characters

    >>> StrictVersion("2.1-rc1") < StrictVersion("2.2")
    Traceback (most recent call last):
      File "", line 1, in 
      File "C:\Python27\lib\distutils\version.py", line 40, in __init__
        self.parse(vstring)
      File "C:\Python27\lib\distutils\version.py", line 107, in parse
        raise ValueError, "invalid version number '%s'" % vstring
    ValueError: invalid version number '2.1-rc1'
    

提交回复
热议问题