简体   繁体   English

Java Pattern和Matcher Regex的好奇心

[英]The Java Pattern and Matcher Regex curiosity

Recently, I have been doing a lot of coding on regex. 最近,我在正则表达式上做了很多编码。 I have assumed the pattern goes this way(code sample1) all along until I tried it as in code sample2: 我一直假设模式一直这样(代码sample1),直到我像代码sample2一样尝试了它:

CODE SAMPLE 1 代码示例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);
    }
}

That works fine, producing the result. 那很好,产生结果。 Meanwhile, when I change the structure as shown below: 同时,当我如下图所示更改结构时:

CODE SAMPLE 2 代码示例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);
}

I realize that sometimes, the sub1 is null and sometimes the sub2 is null. 我意识到有时sub1为null,有时sub2为null。 Any clear, concise explanations as to how Matcher works internally ? 关于Matcher内部运作的任何清晰简洁的解释?

Your first code sample is equivalent to the following: 您的第一个代码示例等效于以下内容:

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); 
}

Basically, Matcher.group returns null when the group that you are referencing (in this case 1 or 2 ) does not have a corresponding position in the search string. 基本上,当您引用的组(在这种情况下为12 )在搜索字符串中没有对应位置时, Matcher.group返回null

The first example prevents the null s from being printed out by guarding the println statement with an if(... != null) check, which this code sample, in the style of your second example, also accomplishes. 第一个示例通过使用if(... != null)检查来保护println语句,从而防止null的输出,此代码示例也可以使用第二个示例的样式来完成检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM