You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Groovy从指定List中提取version的value值?

Extracting Version Values with Groovy

Here's a straightforward approach using regular expressions to pull out the version values from your input string:

// Your input string
def input = '''[version[attributes={}; value=[3.0.2-open-SNAPSHOT]], version[attributes={}; value=[3.0.10-ALIYUN-SNAPSHOT]], version[attributes={}; value=[3.0.11-SNAPSHOT]], version[attributes={}; value=[3.2.0-SNAPSHOT]], version[attributes={}; value=[3.2.4-SNAPSHOT]], version[attributes={}; value=[3.6.1-SNAPSHOT]], version[attributes={}; value=[3.6.2-SNAPSHOT]], version[attributes={}; value=[3.6.2-ForEagleEye-SNAPSHOT]]]'''

// Use regex to capture all value parts inside value=[...]
def versionMatcher = input =~ /value=\[([^\]]+)\]/

// Collect all matched values and join them with commas
def extractedVersions = versionMatcher.collect { match -> match[1] }.join(', ')

// Wrap in brackets to get the desired format
def finalResult = "[$extractedVersions]"

// Print or use the result
println(finalResult)

When you run this script, the output will be:

[3.0.2-open-SNAPSHOT, 3.0.10-ALIYUN-SNAPSHOT, 3.0.11-SNAPSHOT, 3.2.0-SNAPSHOT, 3.2.4-SNAPSHOT, 3.6.1-SNAPSHOT, 3.6.2-SNAPSHOT, 3.6.2-ForEagleEye-SNAPSHOT]

How it works:

  • Regex Pattern: /value=\[([^\]]+)\]/ targets the value=[...] segments, capturing everything inside the inner brackets (the version string) using a capturing group.
  • Collect Matches: The collect method grabs each captured version string from the regex matches.
  • Join & Format: We join the list of versions with commas and wrap the result in square brackets to match your desired output.

If your input follows a consistent structure, this regex approach is quick and effective. If you're dealing with more complex nested structures later on, you might consider parsing the input as a structured object (like converting it to JSON first if possible), but for this specific case, regex is the simplest solution.

内容的提问来源于stack exchange,提问作者Saranya Unni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:49:04