当前位置: 动力学知识库 > 问答 > 编程问答 >

groovy - Regex for Version Number Format

问题描述:

Could you please help in giving me the regex for the following version number format:

e.g. 10.01.03-13

< major >.< minor >.< patch >-< buildnumb >

网友答案:

You can use a simple match on digits following the pattern of digit-dot-digit-dot-digit-hyphen-digit, since groovy uses the java engine.

(\d+)\.(\d+)\.(\d+)\-(\d+)

Test

#1 = major

#2 = minor

#3 = patch

#4 = buildnumb

网友答案:

You can also use named groups if you fancy:

def version = '10.01.03-13'

def parser = /(?<major>\d+).(?<minor>\d+).(?<revision>\d+)-(?<build>\d+)/

def match = version =~ parser
if(match.matches()) {
    def (major, minor, revision, build) = ['major', 'minor', 'revision', 'build'].collect { match.group(it) }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: