groovy "matches" 正则表达式

groovy "matches" regex

我们有这样的不同版本

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

我们尝试在正则表达式下方使用它,它为所有以上版本返回“true”,请帮助更正正则表达式,其中第 1-3 版本以上是真正的“发布”版本。对于第 4、5 和 6 行,我们想为发布版本设置“false”(有很多变种,对于第 4、5、6 行,第一个点之后的字符串与第 1、2、3 看起来不一样。

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

我会这样放置代码:

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 )}"
}

打印

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