Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Version Number Format

Tags:

regex

groovy

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 >

like image 944
Ebrahim Moshaya Avatar asked Dec 25 '22 05:12

Ebrahim Moshaya


2 Answers

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

like image 61
ʰᵈˑ Avatar answered Jan 04 '23 10:01

ʰᵈˑ


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) }
}
like image 45
tim_yates Avatar answered Jan 04 '23 11:01

tim_yates