Does anybody know where in Pattern API the behaviour of this line of code is described
System.out.println("000".matches("(0)\\10“));
I think few people can say what it prints until they run it. API says
\n Whatever the n-th capturing group matched
It does not say that n must be 1 digit. Is it 10-th or 1-th group in my test?
You attempt to match the character
0between parenthesis, and then you want the previous matched character\1to be there also, followed by a0character.000does verify that pattern and thus thematch()method returnstrue, so it printstrue.Since the matcher did not found
10capturing groups, it interprets it as the first one\1then the character0.A more complex example shows that if the matcher find
Ncapturing group > 9 and that the available number of capturing groups is enough, it works also:Is true because
0is in the first capturing group\1and11is in the capturing group\12, finally there is no captured group number\30so it is interpreted as back reference\3(which is character2) then the character0.