英文:
String filtering
问题
以下是您的翻译内容:
我有一个类似于Java的Groovy脚本,用于Jenkins流水线。
在这里,我有以下情况。
有一个包含脚本版本的字符串的ArrayList,例如:
def version_list = [] as ArrayList<String>
-> [Version-2.5.0.0, Version-2.4.0.0, Version-2.3.1.0, Version-2.3.0.0, Version-2.0.0.0]
这个列表是固定的,不能被修改。
我正在遍历列表以获取每个元素,执行一些处理。
另外,我需要检查每个元素,是否存在相同版本的点版本,即:
version_list.each{each_version ->
if(version_list.contains(2.3.x.0)){ //当x!=0
print("版本 " + each_version + " 有点发布")
}
}
我已经尝试了以下方法,但这不起作用。
def version_list = [] as ArrayList<String>
def splitted_ver = [] as ArrayList<String>
version_list.each{each_version ->
splitted_ver = each_version.split("\\.")
if (version_list.contains("^" + splitted_ver[0] + "." + splitted_ver[1] + "(.([1-9]+)\\.([0-9]+)$)")){
print(each_version + " 有点发布!")
}else{
print("没有点发布")
}
}
英文:
I have a Java like Groovy script, using for Jenkins Pipelines.
In there I have the following scenario.
There's a ArrayList with Strings, specifying versions of a script, such as:
def version_list = [] as ArrayList<String>
-> [Version-2.5.0.0, Version-2.4.0.0, Version-2.3.1.0, Version-2.3.0.0, Version-2.0.0.0]
This list is fixed and cannot be modified.
I am iterating over the list to get each element, to do some processing.
Additionally I need to check for each element, if there is a point release of the same version i.e.:
version_list.each{each_version -> //each_version = Version-2.3.0.0
if(version_list.contains(2.3.x.0)){ //while x!=0
print("Version " + each_version + "has point_release")
}
}
I already tried the following, but this is not working.
def version_list = [] as ArrayList<String>
def splitted_ver = [] as ArrayList<String>
version_list.each{each_version ->
splitted_ver = each_version.split("\\.")
if (version_list.contains("^" + splitted_ver[0] + "." + splitted_ver[1] + "(.([1-9]+)\\.([0-9]+)$)")){
print(each_version + " has a point_release!")
}else{
print("has no point_release")
}
}
答案1
得分: 1
我相信你是想要类似这样的代码
//示例列表
def version_list = ["Version-2.2.0.0","Version-2.2.1.0","Version-2.1.1.0","Version-2.1.0.0","Version-2.3.0.0"]
//按版本的前两位数字分组
def ver_map = version_list.groupBy{ ver -> ver.substring(8,11) }
ver_map.each{ k, v ->
//打印每组中的最低版本(根版本),
//然后确定是否有点版本
println new StringBuilder().append(v.min{it}).append(" ").append(v.any{
it.matches("^Version-([0-9])\\.([0-9])\\.([1-9])\\.([0-9])$")}
? "有点版本发布!" : "没有点版本发布")}
在这个示例中,输出如下
Version-2.2.0.0 有点版本发布!
Version-2.1.0.0 有点版本发布!
Version-2.3.0.0 没有点版本发布
英文:
I believe you're going for something like this
//example list
def version_list = ["Version-2.2.0.0","Version-2.2.1.0","Version-2.1.1.0","Version-2.1.0.0","Version-2.3.0.0"]
//group by version first 2 digits
def ver_map = version_list.groupBy{ ver -> ver.substring(8,11) }
ver_map.each{ k, v ->
//prints the lowest (root) version in each set,
//then determines if any has point version
println new StringBuilder().append(v.min{it}).append(" ").append(v.any{
it.matches("^Version\\-([0-9])\\.([0-9])\\.([1-9])\\.([0-9])$")}
? "has a point release!" : "has no point release")}
In this example, prints out
Version-2.2.0.0 has a point release!
Version-2.1.0.0 has a point release!
Version-2.3.0.0 has no point release
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论