2017-03-06 90 views
0

我在正则表达式中很差。我搜索了一下,并对其有了基本的了解。Java中的模式匹配问题

我有以下要求: 我的命令可能包含一些带有“$(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"); 
    } 

回答

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 
+0

它正在工作。谢谢@Adam – CharanTej

1

您可以使用Pattern.quote("Q$(w+)E")方法在编译方法中添加Pattern来传递。

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

谢谢扯谈。但是,是不是为我工作。 – CharanTej

1

我认为你过于复杂的问题。
由于$(是保留 “字”,只是这样做是为了检查是否有出现:

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"); 
     } 
    } 
} 
+0

谢谢你的回复LppEdd。但是,实际上我正在用环境变量替换匹配的模式。我不想改变这个实现(它已经被处理以替换匹配的模式) 我的要求是,在解析出现在我的列表中的所有变量(代码中的WORDS)之后,再次必须交叉检查是否全部$(XXX)已解决。 类似这样的: cmd =“somescript.file $(ABC_PATH1)$(ENV_PATH2)$(ENV3)”; >>>(ABC_PATH1)>>>“WORD1”, $(ENV_PATH2)>>>“WORD2” $(ENV3)>>>没有可用于此目的。 – CharanTej

+0

嗨!我仍然不确定你想要做什么。你需要展示各种占位符吗? – LppEdd

+0

@CharanTej查看更新回答 – LppEdd

1

的模式可能是这样的:

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 
+0

感谢您的快速回复。它仅适用于两个参数。 但是,对于下面,它不工作。 final String command "somescript.file $(ABC_PATH1) $(ENV_PATH2) $(ENV_PATH3) "; CharanTej

+0

对不起,这是我的错。这是工作。谢谢你,@Krzysztof Cichocki – CharanTej

1

的问题是,引用适用于\ w +在模式以及我认为这不是内部(因为它是匹配包含反斜线,'w'和加号的字符串“cmd $(\ w +)”)。

模式可以被替换为:

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

或者,如果你仍然想使用\ Q和\ E对第一部分:

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

它的工作。谢谢你,谢谢。 – CharanTej