What is the result? A. 234 B. 334 C. 2334 D. 0123456 E. 01234456 F. 12334567 G. Compilation fails.
E is correct. The \d is looking for digits. The * is a quantifier that looks for 0 to many occurrences of the pattern that precedes it. Because we specified *, the group() method returns empty Strings until consecutive digits are found, so the only time group() returns a value is when it returns 34 when the matcher finds digits starting in position 2. The start() method returns the starting position of the previous match because, again, we said find 0 to many occurrences.
Why this code doesnt fail, shouldnt be the regex expresion something like "\\d*" instead "\d*" in order to inform the compiler that this is not an escape character
The "\\d*" is required inside source code for the compiler, when you compile the code the String is converted to \d* which has 3 characters by the compiler. regex requires the string as \d* What you give as command line arguments is a string given at runtime and it taken as is.
I have a different question for the code sample above.
I didn't expect the trailing 6 at the end of that answer. I thought it would be 0123445 and not 01234456. Once the m.group() displays 34, the next output of 4 is the result of m.find() finding a match for the character e. Then 5 gets outputted from m.find() finding a match for the character f.