【问题标题】:Java Comparing two strings with placeholder valuesJava比较两个字符串与占位符值
【发布时间】:2016-10-24 02:16:52
【问题描述】:

我正在为 Java 项目开发基于命令的功能,但在为这些命令引入参数时遇到了麻烦。

例如所有的命令都是这样存储的:

"Hey tell [USER] to [ACTION]"

现在,当用户提交他们的命令时,它将如下所示:

"Hey tell Player to come see me"

现在我需要知道如何将用户输入的命令与包含占位符值的存储命令进行比较。我需要能够比较两个字符串并识别它们是相同的命令,然后从中提取数据 [USER] 和 [ACTION] 并将它们作为数组返回

array[0] = "Player"
array[1] = "come see me"

真的希望有人能帮帮我,谢谢

【问题讨论】:

  • 你能澄清一下是什么问题吗?您可以使用 string.split(" ") 从这样的句子中创建一个数组,并使用 string1.equals(string2) 比较两个字符串。
  • 您是否尝试过使用连接“嘿告诉”+ [USER] +“to”+“[ACTION]”
  • 好吧,基本上我有一个数组列表,其中包含带有占位符值的命令。例如“嘿,告诉 [USER] 到 [ACTION]”。现在,当用户提交诸如“Hey tell John to come see me”之类的命令时,我需要能够将其与 arraylist 中存储的命令进行比较,识别它是相同的命令,并从命令“John ”和“来看我”。

标签: java string command


【解决方案1】:

您可以使用如下模式匹配:

    String command = "Hey tell [USER] to [ACTION]";
    String input = "Hey tell Player to come see me";
    String[] userInputArray = new String[2];

    String patternTemplate = command.replace("[USER]", "(.*)"); 
    patternTemplate = patternTemplate.replace("[ACTION]", "(.*)");

    Pattern pattern = Pattern.compile(patternTemplate);
    Matcher matcher = pattern.matcher(input);
        if (matcher.matches()) {
            userInputArray[0] = matcher.group(1);
            userInputArray[1] = matcher.group(2);

        } 

【讨论】:

    【解决方案2】:

    如果您不需要像“Hey tell [USER] to [ACTION]”这样的存储字符串,您可以使用 Java (java.util.regex) 模式和匹配器。

    这是一个例子:

    Pattern p = Pattern.compile("Hey tell ([a-zA-z]+) to (.+)");
    List<Pattern> listOfCommandPattern = new ArrayList<>();
    listOfCommandPattern.add(p);
    

    例如,解析命令:

    String user;
    String command;
    Matcher m;
    // for every command
    for(Pattern p : listOfCommandPattern){
       m = p.matcher(inputCommand);
       if (m.matches()) {
           user = m.group(1);
           command = m.group(2);
           break; // found user and command
       }
    }
    

    【讨论】:

      【解决方案3】:

      这里有一个更通用的版本:

      String pattern = "Hey tell [USER] to [ACTION]";
      String line = "Hey tell Player to come see me";
      
      /* a regular expression matching bracket expressions */
      java.util.regex.Pattern bracket_regexp = Pattern.compile("\\[[^]]*\\]");
      
      /* how many bracket expressions are in "pattern"? */
      int count = bracket_regexp.split(" " + pattern + " ").length - 1;
      
      /* allocate a result array big enough */
      String[] result = new String[count];
      
      /* convert "pattern" into a regular expression */
      String regex_pattern = bracket_regexp.matcher(pattern).replaceAll("(.*)");
      java.util.regex.Pattern line_regex = Pattern.compile(regex_pattern);
      
      /* match "line" */
      if (line_regex.matcher(line).matches()) {
          /* extract the matched strings */
          for (int i=0; i<count; ++i) {
              result[i] = line_matcher.group(i+1);
              System.out.println(result[i]);
          }
      } else {
          System.out.println("Doesn't match.");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-18
        • 2013-03-23
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 2019-10-05
        相关资源
        最近更新 更多