I'm trying to convert a string into a map of values using regex and known delimiters. The code I have works, but if I use a delimiter which is a substring of another delimiter, it is not parsed (properly).
Let's cut straight to some sample input, erroneous output, expected output, and code!
Sample input: "Artist: foo bar foooo Title: bar fooo bar Dimensions: x z y Framed dimensions: y z x"
(as you can see there is "Dimensions" and "Framed dimensions")
Erroneous output: {Artist:=foo bar foooo, Title:=bar fooo bar, Dimensions:=x z y, dimensions:=y z x}
(Framed dimensions got caught under dimensions!)
Expected output: Artist:=foo bar foooo, Title:=bar fooo bar, Dimensions:=x z y, Framed dimensions:=y z x}
Code example:
String DELIMITER = "[Aa]rtist:|[Tt]itle:|[Ff]ramed [Dd]imensions:|[Dd]imensions:"
...
public Map<String, String> parseToMap(String str) {
Map<String, String> itemMap = new LinkedHashMap<>();
String infos[] = str.split("(?=" + DELIMITER + ')'); //split at delimiters
for(String info : infos) {
try {
String[] tmp = info.split("(?<=" + DELIMITER + ')'); //split to key/val pair
itemMap.put(tmp[0].trim(), tmp[1].trim());
} catch (IndexOutOfBoundsException e) {
//Skip if no key/val pair
}
}
return itemMap;
}
I also feel like this is a bit hackish. If there is a more elegant solution, I'd be glad to hear it. Although I can always make a trip to CodeReview if we can just get this working for now :)
EDIT: I need every word from delimiter to delimiter, not just the word following a delimiter.