在Groovy中,如何仅从列表中提取匹配字符串?
在我的groovy脚本中,我有以下列表
subList = ["foo", "bar", "baz"]
,文本文件master_list.txt
带有以下内容:
MASTER_STRING="ping:123 foo:321 pong:999 bar:888"
OTHER_STRING=helloWorld
我需要获取唯一的匹配项表单master_string
,因此上面的示例,我需要获得一个列表 [“ foo:321”,“ bar:888”]
我对Groovy非常新,我可以找到是否在Master_list
中找到的匹配sub字符串,该字符串中的代码> sublist ,但是如何将所需的字符串提取为列表?
props = readProperties file: "master_list.txt"
boolean exists = false
if (props.containsKey('MASTER_STRING')) {
def masterList = props.MASTER_STRING.split(/ /)
exists = masterList.findAll { a ->
subList.any { a.contains(it)}
}
}
if (exists) {
println "at least one item exists" // Here I like to print ["foo:321", "bar:888"]
}
In my groovy script, I have a list like below
subList = ["foo", "bar", "baz"]
and a text file master_list.txt
with below content:
MASTER_STRING="ping:123 foo:321 pong:999 bar:888"
OTHER_STRING=helloWorld
I need to get the only the matching items form MASTER_STRING
, so from above example, I need to get a list like["foo:321", "bar:888"]
I am very new to groovy, I could find if a matching sub string found in MASTER_LIST
which is there in subList
, but how to extract the required strings as list?
props = readProperties file: "master_list.txt"
boolean exists = false
if (props.containsKey('MASTER_STRING')) {
def masterList = props.MASTER_STRING.split(/ /)
exists = masterList.findAll { a ->
subList.any { a.contains(it)}
}
}
if (exists) {
println "at least one item exists" // Here I like to print ["foo:321", "bar:888"]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用读取文件显示它,而不是有2个列表。
打印
[AAA:123,BBB:002]
,这就是我想要的,我在问题中对此非常复杂。感谢@matt的评论。Showing it withoug reading files, instead having 2 lists.
prints
[aaa:123, bbb:002]
and this is what I want, I overly complicated this in my question. thanks @Matt for your comment.