hare krshn
hare krshn

Reputation: 386

groovy "matches" regex

we have different versions like this

20.0.19.198
18.7b.0.2
19.1.1b.1
20.acme.0.234
20.xyzname.0.123
20.helloworld.0.345

We tried using this below regex, it is returning "true" for all above versions, please help to correct the regex where above 1st-3rd versions are true "release" versions". For 4th, 5th and 6th lines, we want to make "false" for release versions (theres many varieties where , for 4th, 5th, 6th line, the string after 1st dot is not like how 1st,2nd,3rd looks.

def isReleaseVerson(String version){
    return version.matches("(\\d+)\\.(.*)\\.(\\d+)")
}

Upvotes: 0

Views: 226

Answers (1)

injecteer
injecteer

Reputation: 20699

I'd put the code like so:

boolean isReleaseVersion( String s ) {
  s ==~ /^\d+\.\d+\w*\.\w+\.\d+$/
}

def list = '''\
  20.0.19.198
  18.7b.0.2
  19.1.1b.1
  20.acme.0.234
  20.xyzname.0.123
  20.helloworld.0.345'''.stripIndent().readLines().each{
  println "$it -> ${isReleaseVersion( it )}"
}

prints

20.0.19.198 -> true
18.7b.0.2 -> true
19.1.1b.1 -> true
20.acme.0.234 -> false
20.xyzname.0.123 -> false
20.helloworld.0.345 -> false

Upvotes: 1

Related Questions