如何使用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 thevalue=[...]segments, capturing everything inside the inner brackets (the version string) using a capturing group. - Collect Matches: The
collectmethod 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
相关产品推荐
相关产品推荐

