I’m trying to establish some way of mapping a String document to a HashMap as follows:
the String contains key/value pair
$key1=value1
$key2=value2 value21
value22
$key3=value3
what I want to end up with is:
key1, value1
key2, value2 value21\nvalue22
key3, value3
Is there a pattern I can use for this? It looks like an interesting puzzle, so far I have come up with using split("[$]{1}[A-Za-z]+[=]{1}") to separate the different values but then it has to be a different iteration to identify the keys, so I’m looking for a more elegant solution.
You must use two regex here:
\$(\w+)=((\w+\s*)+)will separate keys from values(\s+)to split values.NB: You could use
\$(\w+)=(.*)as regex too, it depends on what you want to match, in the case above, every word/number separated by spaces, in this case, anything.