You may easily get the key value pairs by splitting them with |
(optionally enclosed with whitespace), iterate over the array and split with :
(also optionally enclosed with whitespace).
See an example Java code:
Map<String, String> map = new HashMap<String, String>();
String test = "[xx] Guidance) Boundary: 123 | Total: 1010 | Steps Count test: 21 | initial value Count: 19";
String[] kvps = test.split("\\s*\\|\\s*"); // split on 'spaces|spaces'
for (int i = 0; i < kvps.length; i ++) {
String[] kvp = kvps[i].split("\\s*:\\s*");
map.put(kvp[0], kvp[1]);
}
// DEMO OUTPUT
for (String s : map.keySet()) {
System.out.println(s + " => " + map.get(s));
}
Result:
initial value Count => 19
[xx] Guidance) Boundary => 123
Total => 1010
Steps Count test => 21
A matching regex solution will be probably too cumbersome and unsafe. E.g. you might try
([^:]+?)\s*:\s*([^\s|]+)
See the regex demo. It is as generic as possible, but relies on some assumptions: there can be no :
before the :
delimiter and the key cannot be empty, and the value cannot have whitespace and |
char. If the key can be empty, replace ([^:]+?)
with ([^:|]*?)
. If the value can contain whitespace, replace ([^\s|]+)
with ([^|]+)
. Note you will need to trim()
the values after matching them. See another regex demo.