字符串过滤

huangapple go评论67阅读模式
英文:

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&lt;String&gt;
-&gt; [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 -&gt; //each_version = Version-2.3.0.0
                    if(version_list.contains(2.3.x.0)){ //while x!=0
                       print(&quot;Version &quot; + each_version + &quot;has point_release&quot;)
                    }
                }

I already tried the following, but this is not working.

 def version_list = [] as ArrayList&lt;String&gt;
 def splitted_ver = [] as ArrayList&lt;String&gt;
 
 version_list.each{each_version -&gt;
                        splitted_ver = each_version.split(&quot;\\.&quot;)
                        if (version_list.contains(&quot;^&quot; + splitted_ver[0] + &quot;.&quot; +  splitted_ver[1] + &quot;(.([1-9]+)\\.([0-9]+)$)&quot;)){
                            print(each_version + &quot; has a point_release!&quot;)   
                        }else{
                            print(&quot;has no point_release&quot;)
                        }
                    }

答案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 = [&quot;Version-2.2.0.0&quot;,&quot;Version-2.2.1.0&quot;,&quot;Version-2.1.1.0&quot;,&quot;Version-2.1.0.0&quot;,&quot;Version-2.3.0.0&quot;]
//group by version first 2 digits
def ver_map = version_list.groupBy{ ver -&gt; ver.substring(8,11) }

ver_map.each{ k, v -&gt;
    //prints the lowest (root) version in each set, 
    //then determines if any has point version
    println new StringBuilder().append(v.min{it}).append(&quot; &quot;).append(v.any{ 
        it.matches(&quot;^Version\\-([0-9])\\.([0-9])\\.([1-9])\\.([0-9])$&quot;)} 
            ? &quot;has a point release!&quot; : &quot;has no point release&quot;)}

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

huangapple
  • 本文由 发表于 2023年3月3日 20:27:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定