【发布时间】:2019-09-16 13:59:42
【问题描述】:
目前有一个不寻常的问题,我正在遍历标签列表并针对正则表达式模式进行测试以查看它是否匹配。
def tags = sh ( script: "git ls-remote --tags git@bitbucket.org:<PATH_TO_GIT_REPO>.git ", returnStdout: true)
def tag_line = tags.split("\n")
tag_line.each
{
tag ->
// println(tag)
current_tag = tag.split()[1].split("/")[2].trim()
println("Current Line: " + current_tag)
def major = (current_tag =~ /v.+\.\d+\.\d*/)
println(major)
}
如果我使用正则表达式在线测试器,则会返回匹配项。但是无论我做什么,控制台的输出如下:
Current Line: v1.9.0^{}
[Pipeline] echo
java.util.regex.Matcher[pattern=v.+\.\d+\.\d* region=0,9 lastmatch=]
[Pipeline] echo
Current Line: v1.9.1
[Pipeline] echo
java.util.regex.Matcher[pattern=v.+\.\d+\.\d* region=0,6 lastmatch=]
【问题讨论】:
-
你到底需要得到什么?
v1.9.1? -
好吧,老实说,我最初是使用捕获组来抓取第一部分,但只是简化了它以查看它是否匹配任何内容。在这种情况下,是的,只需匹配 '''v1.9.1'''。
-
然后添加
if (major) { println major.group() } -
不应该 lastmatch 返回一些东西吗?期望它包含一些东西,对于在 Jenkins 中使用正则表达式来说非常新。
-
不,
(current_tag =~ /v.+\.\d+\.\d*/)是一个尚未运行的匹配器。