Groovy small text manipulation

I have the following string:

[bla.FormatException, 96][bla, 22][ResourceNotFoundException, 48]

i need to make it look like this:

['bla.FormatException', 96],
['bla', 22],
['ResourceNotFoundException', 48]

right now im doing this:

textToAdd.replace("][","],\n[").replace("[","['").replace(", ","', ")

it works but it looks amatur and could break if a small change would take place even extra space.. can i use regex for manipulation to make more clear?


Solution 1:

Regex and multiple assignment are here to help:

String text = '[bla.FormatException, 96][bla, 22][ResourceNotFoundException, 48]'

String result = ( text =~ /(\[([^,\[\]]+), (\d+)\])/ ).findAll().collect{ _, __, name, number -> "['$name', $number]" }.join( ',\n' )

assert result == '''\
['bla.FormatException', 96],
['bla', 22],
['ResourceNotFoundException', 48]'''