Groovy 从字符串创建 2 个暗数组 [][]

Groovy create 2 dim array [][] from string

string = """
name:bla1-service,
  version:Sprint-91.0.0
  
  name:bla2-service,
  version:Sprint-93.0.0
  
  name:cbla3-service,
  version:Sprint-93.0.0
  
  name:bla4-service,
  version:Sprint-93.0.0
  
"""

最终结果应该是一个数组

myArray = [[bla1-service,version:Sprint-91.0.0],[bla2-service,Sprint-93.0.0] ...]

我可以使用 for 循环来搜索像“名称”和“版本”这样的字符串并构建它,但我认为必须有更简单的方法(也许使用 split(",")?) 谁能提出更好的方法?

一些 straight-forward 正则表达式:

String string = """
name:bla1-service,
  version:Sprint-91.0.0
  
  name:bla2-service,
  version:Sprint-93.0.0
  
  name:cbla3-service,
  version:Sprint-93.0.0
  
  name:bla4-service,
  version:Sprint-93.0.0
  
"""

def res = []
string.eachMatch( /name:([^,]+),\s+version:(\S+)/ ){ _, name, version -> res << [ name, version ] }

assert res.toString() == '[[bla1-service, Sprint-91.0.0], [bla2-service, Sprint-93.0.0], [cbla3-service, Sprint-93.0.0], [bla4-service, Sprint-93.0.0]]'