Java模式和匹配器正则表达式的好奇心

时间:2015-08-07 16:53:23

标签: java regex

最近,我一直在对正则表达式进行大量编码。我假设模式一直这样(代码sample1),直到我尝试它,如代码sample2:

代码示例1

Pattern pattern = Pattern.compile("^([\\w]+)(?=\\s)|(?<=\\*)(.+?)(?=\\)|$)");
Matcher matcher = pattern.matcher(word);
String sub1 = null;
String sub2 = null;

while (matcher.find()) {
    if (matcher.group(1) != null) {
        sub1 = matcher.group(1);
        System.out.println(sub1);
    }
    else if (matcher.group(2) != null) {
        sub2 = matcher.group(2);
        System.out.println(sub2);
    }
}

工作正常,产生结果。同时,当我改变结构如下所示:

代码示例2

Pattern pattern = Pattern.compile("^([\\w]+)(?=\\s)|(?<=\\*)(.+?)(?=\\)|$)");
Matcher matcher = pattern.matcher(word);
//note please, though I have taken out the String definition it still gives the same result even If I had defined them here like I did in code1
while (matcher.find()) {
    String sub1 = matcher.group(1);
    String sub2 = matcher.group(2);
    System.out.println(sub1);
    System.out.println(sub2);
}

我意识到有时候,sub1为null,有时sub2为null。关于Matcher如何在内部工作的任何清晰,简洁的解释?

1 个答案:

答案 0 :(得分:0)

您的第一个代码示例等同于以下内容:

Pattern pattern = Pattern.compile("^([\\w]+)(?=\\s)|(?<=\\*)(.+?)(?=\\)|$)");
Matcher matcher = pattern.matcher(word);
while (matcher.find()) {
     String sub1 = matcher.group(1);
     String sub2 = matcher.group(2);
     if(sub1 != null) System.out.println(sub1);
     else if(sub2 != null) System.out.println(sub2); 
}

基本上,当您引用的组(在本例中为Matcher.groupnull)在搜索字符串中没有对应的位置时,1会返回2

第一个示例通过使用null检查保护println语句来阻止if(... != null)打印出来,此代码示例以第二个示例的样式也是实现