简体   繁体   中英

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:

CODE SAMPLE 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

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. Any clear, concise explanations as to how Matcher works internally ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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