Below code :
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestCode {
private Map<String, String> getMappedValues(final String line) {
final Map<String, String> mappedValues = new HashMap<String, String>();
final Pattern p = Pattern.compile("\"(.*?)\"");
final Matcher m = p.matcher(line);
while (m.find()) {
for (String strTemp : m.group().split(",")) {
String key = strTemp.split("=")[0].replace("$", "").replace("\"", "").trim();
String value = strTemp.split("=")[1].replace("\"", "").trim();
mappedValues.put(key, value);
}
}
return mappedValues;
}
public static void main(String args[]) {
final String str = "aaaa,\"$0 = test1, $1 = test2\",a,b,c";
final TestCode testCode = new TestCode();
Map<String, String> mappedValues = testCode.getMappedValues(str);
mappedValues.entrySet().forEach(entry -> {
System.out.println(entry.getKey() + " " + entry.getValue());
});
}
}
prints :
0 test1
1 test2
The string \"$0 = test1, $1 = test2\"
is parsed from the string
"aaaa,\"$0 = test1, $1 = test2\",a,b,c"
The string \"$0 = test1, $1 = test2\"
is then converted to a map where key is left of =
and value is right of =
A variable number of key value pairs can occur.
I'm just concentrating on the happy path for now.
Is there a cleaner method of parsing the values rather than using :
String key = strTemp.split("=")[0].replace("$", "").replace("\"", "").trim();
String value = strTemp.split("=")[1].replace("\"", "").trim();
?