【问题标题】:Pattern matching issue in JavaJava中的模式匹配问题
【发布时间】:2017-03-06 13:04:44
【问题描述】:

我的正则表达式很差。我google了一下,对它有了基本的了解。

我有以下要求: 我的命令可能包含一些带有“$(VAR_NAME)”模式的字符串。我需要找出它是否有这种类型的字符串。如果是这样,我必须解决这些问题(如果有这样的字符串,我知道我应该怎么做)。 但是,问题是,如何查找命令是否包含带有“$(VAR_NAME)”模式的字符串。我的命令中可能有多个或零个这样的字符串模式。

据我所知,我编写了以下代码。如果我在下面的代码中使用 'pattern1' ,它是匹配的。但是,不是'pattern' 有人可以帮忙吗?

提前谢谢你。

    final String command = "somescript.file $(ABC_PATH1) $(ENV_PATH2) <may be other args too here>";
    final String pattern = "\\Q$(\\w+)\\E";
    //final String pattern1 = "\\Q$(ABC_PATH1)\\E";

    final Pattern pr = Pattern.compile(pattern);
    final Matcher match = pr.matcher(command);
    if (match.find())
    {
        System.out.println("Found value: " + match.group(0));
    }
    else
    {
        System.out.println("NO MATCH");
    }

【问题讨论】:

    标签: java regex pattern-matching


    【解决方案1】:

    使用 \Q 和 \E 意味着您无法为变量名称设置捕获组,因为圆括号将按字面意思解释。

    我可能会这样做,只是避开外部 $, ( 和 )。

    此外,如果您需要多次匹配,则需要多次调用 find(),我为此使用了 while 循环。

    final String command = "somescript.file $(ABC_PATH1) $(ENV_PATH2) <may be other args too here>";
    final String pattern = "\\$\\((\\w+)\\)";
    
    final Pattern pr = Pattern.compile(pattern);
    final Matcher match = pr.matcher(command);
    while (match.find()) {
        System.out.println("Found value: " + match.group(1));
    }
    

    输出

    Found value: ABC_PATH1
    Found value: ENV_PATH2
    

    【讨论】:

      【解决方案2】:

      您可以使用Pattern.quote("Q$(w+)E") 方法添加Pattern 以传入compile 方法。

       final Pattern pr = Pattern.compile(Pattern.quote("Q$(w+)E"));
      

      【讨论】:

        【解决方案3】:

        模式可能如下所示:

        public static void main(String[] args) {
            final String command = "somescript.file $(ABC_PATH1) $(ENV_PATH2) <may be other args too here>";
            final String pattern = "\\$\\((.*?)\\)";
            // final String pattern1 = "\\Q$(ABC_PATH1)\\E";
        
            final Pattern pr = Pattern.compile(pattern);
            final Matcher match = pr.matcher(command);
        
            while (match.find()) {
                System.out.println("Found value: " + match.group(1));
            }
        
        }
        

        打印:

            Found value: ABC_PATH1
            Found value: ENV_PATH2
        

        【讨论】:

        • 感谢您的快速回复。它仅适用于两个参数。但是,对于下面,它不起作用。 final String command "somescript.file $(ABC_PATH1) $(ENV_PATH2) $(ENV_PATH3) <maybe other args too here>"; </maybe>
        • 对不起,这是我的错误。这是工作。谢谢你,@Krzysztof Cichocki
        【解决方案4】:

        问题是引用也适用于模式中的 \w+,我认为这不是意图(因为它与包含反斜杠的字符串 "cmd $(\w+)" 匹配,'w ' 和加号)。

        模式可以替换为:

            final String pattern = "\\$\\(\\w+\\)";
        

        或者,如果您仍想在第一部分使用 \Q 和 \E:

            final String pattern = "\\Q$(\\E\\w+\\)";
        

        【讨论】:

          【解决方案5】:

          我认为你把问题复杂化了。
          由于$( 是保留的“单词”,因此只需执行此操作即可检查是否有出现:

          command.indexOf("$(");
          

          使用示例:

          public class Test
          {
             private static final String[] WORDS;
          
             static {
                WORDS = new String[] {
                      "WORD1",
                      "WORD2"
                };
             }
          
             public static void main(final String[] args) {
                String command = "somescript.file $(ABC_PATH1) $(ENV_PATH2)";
          
                int index = 0;
                int i = 0;
          
                while (true) {
                   index = command.indexOf("$(", index);
          
                   if (index < 0) {
                      break;
                   }
          
                   command = command.replace(command.substring(index, command.indexOf(")", index) + 1), WORDS[i++]);
                }
             }
          }
          

          打印:somescript.file WORD1 WORD2

          坚持原文:

          public class Test
          {
             public static void main(final String[] args) {
                final String command = "somescript.file $(ABC_PATH1) $(ENV_PATH2)";
                int index = 0;
                int occurrences = 0;
          
                while (true) {
                   index = command.indexOf("$(", index);
          
                   if (index < 0) {
                      break;
                   }
          
                   occurrences++;
                   System.out.println(command.substring(index, command.indexOf(")", index++) + 1));
                }
          
                if (occurrences < 1) {
                   System.out.println("No placeholders found");
                }
             }
          }
          

          【讨论】:

          • 感谢 LppEdd 的回复。但是,实际上我正在用环境变量替换匹配的模式。而且我不想更改该实现。(它已经被处理以替换匹配的模式)我的要求是,在解决了我的列表中存在的所有变量之后(代码中的 WORDS),再次必须交叉检查是否所有 $ (XXX) 已解决。像这样: cmd = "somescript.file $(ABC_PATH1) $(ENV_PATH2) $(ENV3)"; $(ABC_PATH1) >>> "WORD1", $(ENV_PATH2) >>> "WORD2" $(ENV3) >>> 没有可用的。
          • 嗨!我仍然不确定你想做什么。是否需要显示各种占位符?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-03
          • 1970-01-01
          • 2011-05-31
          • 1970-01-01
          • 1970-01-01
          • 2011-06-17
          • 2020-07-05
          相关资源
          最近更新 更多