Given two strings
String command = "Header '{1}' has a value that ends with '{2}' (ignore case)";
String input = "Header 'some-value' has a value that ends with '123ws' (ignore case)";
I'd like to get value map.
0 -> some-value
1 -> 123ws
I referenced this answer on Java Comparing two strings with placeholder values, tweaked it little for my usage.
private static Map<Integer, Object> getUserInputMap(String command, String input) {
System.out.println("\t" + command);
System.out.println("\t" + input);
command = command.replace("(", "<");
command = command.replace(")", ">");
input = input.replace("(", "<");
input = input.replace(")", ">");
Map<Integer, Object> userInputMap = new HashMap<>();
String patternTemplate = command.replace("{0}", "(.*)");
patternTemplate = patternTemplate.replace("{1}", "(.*)");
patternTemplate = patternTemplate.replace("{2}", "(.*)");
Pattern pattern = Pattern.compile(patternTemplate);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
for (int gi = 1; gi <= matcher.groupCount(); gi++) {
String uin = matcher.group(gi);
uin = uin.replace("<", "(");
uin = uin.replace(">", ")");
userInputMap.put(gi - 1, uin);
}
}
return userInputMap;
}
But, there could be many corner cases. My worry with my solution is that I might miss out on a corner case, and then production bug.
Is there any mature library written around this? I am checking MessageFormat/StrSubstitutor but I am not able to get any method which does what I expect.