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
"""
end result should be an array
myArray = [[bla1-service,version:Sprint-91.0.0],[bla2-service,Sprint-93.0.0] ...]
i can use a for loop to search for string like "name" and "version" and build this but i figure there must be a simpler way (maybe using split(",")?) can anyone suggest a better way?
CodePudding user response:
Something straight-forward with regex:
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]]'