0

给定两个字符串

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 Comparing two strings with placeholder values 上引用了这个答案,对我的使用做了一点调整。

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/StrSubstitutor 但我无法获得任何符合我期望的方法。

4

1 回答 1

2

从已经格式化的字符串中获取除字符串之外的任何内容都不是那么容易,我不会在这里处理。

您基本上知道占位符的格式,即{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",在这种情况下,如果没有更多信息(只有占位符没有这些信息),您将无法安全地确定这两个参数的值。

于 2021-03-23T09:37:54.093 回答