Regex for parsing version number

前端 未结 4 1646
失恋的感觉
失恋的感觉 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: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']
    

提交回复
热议问题