Please explain the working of * greedy quantifier

后端 未结 2 1620
孤街浪徒
孤街浪徒 2021-01-22 02:13
Pattern ptn  = Pattern.compile(\"a*\");
Matcher mtch  = ptn.matcher(\"bbaac\");
if(mtch.find()){
    System.out.println(mtch.group());
}

Output - print

2条回答
  •  时光取名叫无心
    2021-01-22 02:50

    The issue is that there are 5 matches, and you only check for the first one which is an empty string as a* can match an empty string (to be more exact, it matches the empty space before the character it cannot match and at the end of string).

    Use while instead of if.

    See IDEONE demo:

    String s = "bbaac";
    Pattern pattern = Pattern.compile("a*");
    Matcher matcher = pattern.matcher(s);
    while (matcher.find()){
        System.out.println(matcher.group(0)); 
    } 
    

    The number of matches is 5:

    • empty string before the first b
    • empty string before the second b
    • aa
    • empty string before the c and
    • empty (end of) string after c.

    Using a+, with the + quantifier meaning 1 or more occurrences, no empty matches will get extracted and you will only get aa.

    See the empty strings on regex101.com:

提交回复
热议问题