String.split()
takes a regex. If you can put in a regex to match your set of keys, you can use it
Tokenize a String on multiple delimiters [closed]
-
29-03-2022 - |
Question
I want to tokenize a String to key value pairs. But key has characters like underscore, comma, space etc. The key value pair is separated by = character
Example:
key=value start_time="2013-03-01 03:20:40" Key withspace=space1 two withspace=space2 key=value key-key key=value key_underscore=500058 key=value - value key= value_value
Desired output is
Key=value
start_time="2013-03-01 03:20:40"
Key withspace=space1
two withspace=space2
key-key key=value
key_underscore=500058
key=value - value
key= value_value
And this helps me to populate the value in a map and put it in a table.
Can i use any existing methods like StringTokenizer, Patter, String.split() etc. Or should i write my own String parser to get this type of output.
Just want to check some best methods to acheive this
Solution
OTHER TIPS
Key with spaces will not work, at least with the given rules. key=value - value key= value_value
is ambiguos. The last key-value pair can be extracted as key = value_value
or value key= value_value
.