【问题标题】:Regexp for matching executable version number匹配可执行版本号的正则表达式
【发布时间】:2016-06-08 09:44:51
【问题描述】:

我正在尝试编写一个正则表达式来匹配可执行版本号。
可以通过两种方式提供:

  • MAJOR.MINOR.TINY
  • MAJOR.MINOR.TINY.BUILD_NUMBER

我不需要解析这些值,只需检查匹配。所以,我可以: ^\d+[\.\d+]*$。好的,但是这个正则表达式将匹配具有 5 个或更多版本部分的字符串,即1.2.3.4.5。此外,它将匹配1..2.3 之类的字符串。做类似^[as many digits as you want and only one .]{3,4}$ 这样的事情会很棒。

附:当然,我可以先检查^(\d*)\.(\d*)\.(\d*)$ 之类的东西,如果不匹配,则检查^(\d*)\.(\d*)\.(\d*)\.(\d*)$。但也许有更好(更智能)的方法来做到这一点。

我使用re python 模块来检查匹配:

import re

version_regexp = re.compile(r'^\d+[\.\d+]*$')
versions = ['dsvfsdf',
            '1.a.3',
            '1.2.4',
            '1.4..5.6',
            '0.3.1',
            'a.b.c.d',
            '1,2,3']
for i, sample in enumerate(versions):
    print('Sample[%d]: %s' % (i, True if version_regexp.match(sample) else False))

提前谢谢你!

【问题讨论】:

    标签: python regex


    【解决方案1】:

    正则表达式应该像re.compile(r'^\d+(\.\d+){2,3}$')

    【讨论】:

    • 哎呀,真的很简单。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    相关资源
    最近更新 更多