Regex for parsing version number

前端 未结 4 1641
失恋的感觉
失恋的感觉 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 "<stdin>", line 1, in <module>
      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'
    
    0 讨论(0)
  • 2021-01-29 06:58

    Clarification: I'm assuming you want to parse the numbers, not just match them.

    Why use regexes when a simple split will work just fine?

    '1.3.4.*'.split('.') # => ['1', '3', '4', '*']

    If you want to ensure that there is at least one dot in the string, check the array length to ensure it is larger than 1.

    0 讨论(0)
  • 2021-01-29 06:59

    I think you want something like this,

    (?:(\d+\.[.\d]*\d+))
    

    OR

    (?:(\d+\.(?:\d+\.)*\d+))
    

    DEMO

    >>> import re
    >>> str = 'foobar 1.000, 1.0.00, 1.0.0.000 10 100 foo bar foobar'
    >>> m = re.findall(r'(?:(\d+\.(?:\d+\.)*\d+))', str)
    >>> m
    ['1.000', '1.0.00', '1.0.0.000']
    
    0 讨论(0)
  • 2021-01-29 07:03

    Try this regex:

    ^(\d+\\.)?(\d+\\.)?(\\*|\d+)$
    
    0 讨论(0)
提交回复
热议问题