In the same vein, but a bit clearer IMO (it doesn't clear the spaces inside keys and values):
// compile those only once
val entrySeparator = Regex("""\s*,\s*""")
val keyValueSeparator = Regex("""\s*=\s*""")
selection.removePrefix("[")
.removeSuffix("]")
.split(entrySeparator)
.associate {
val (key, value) = it.split(keyValueSeparator)
key to value
}
Slightly more permissive, but simpler (using trim for the brackets):
selection.trim('[', ']')
.split(entrySeparator)
.associate {
val (key, value) = it.split(keyValueSeparator)
key to value
}
But if you're using regex anyway, you may as well use
Regex.findAll
, it might be simpler in the end