3

I am trying to solve an scjp test about regex.

here is a code...

import java.util.regex.*;

public class TestRegex {
    public static void main(String[] args) {
        Pattern p = Pattern.compile(args[0]);
        Matcher m = p.matcher(args[1]);
        boolean b = false;
        while (b = m.find()) {
            System.out.print(m.start() + m.group());
        }
    }
} 

and

java TestRegex "\d*" ab34ef 

the answer for this test is 01234456. I understood everything except the last output(6). Since the last index in "ab34ef" is 5, how is it possible to be printed 6 ?

Any help ....

Pshemo
  • 122,468
  • 25
  • 185
  • 269
tural
  • 310
  • 4
  • 17

1 Answers1

5

\d* means "zero or more digits," which can actually match nothing. The 6 is a match against the empty string after the last character in the string.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405