是否有一个库,在给定的两个字符串上提供占位符值



给定两个字符串

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)";

我想要一张价值图。

0 -> some-value
1 -> 123ws

我在Java上引用了这个答案。将两个字符串与占位符值进行比较,并对其进行了一些调整。

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;
}

但是,可能会有很多角落的案例。我对解决方案的担忧是,我可能会错过一个角落的案例,然后是生产错误。

有没有成熟的图书馆围绕这一点写的?我正在检查MessageFormat/StrSubstitutior,但我无法获得任何符合我期望的方法。

从已经格式化的字符串中获取字符串以外的任何东西都不是那么容易,我在这里不处理这个问题。

你基本上知道占位符的格式,即{digits},所以你可以用它来分割你的命令:command.split("\{0|[1-9][0-9]*\}"(不允许{01}等(

然后对结果数组中的元素进行迭代,并在input中查找精确匹配。在执行此操作时,您希望跟踪结束索引以从那里开始搜索,而不是再次跟踪input的开始。

快速简单的示例(未测试(:

String[] parts = command.split("\{0|[1-9][0-9]*\}");
int paramStart = 0;
int index = 0;
for( String part : parts ) {  
index = input.indexOf(part, index);
if( index < 0) {
//problem: command part hasn't been found - you probably want to end parsing here
} 
//the first part should be found at index 0 so ignore that
if( index != 0 )
//parameter value should be between the last part and the current one
String parameterValue = input.substring(paramStart, index);
} 
//the next parameter starts after the current part
paramStart= index + part.length();
}
//there seems to be a last placeholder at the end of the command
if(paramStart < input.length() - 1) {
//get the substring here
}

这应该能够处理大多数情况,除了那些参数看起来像命令部分或占位符相邻且无法区分的情况。以"{1} - {2}"和参数"A - B""C - D"为例,结果将是"A - B - C - D",在这种情况下,如果没有更多信息(只有占位符就没有(,就无法安全地确定这两个参数的值。

最新更新