It seems that the regex escaping works different in different versions of Java.
In Java openjdk 16.0.1 the compilation works fine
In Java openjdk 11.0.11 this compilation error is thrown:
test.java:15: error: illegal escape character
if (variable.matches("\s*")){
I know, that I'm generally on the safe side with \\
. My question:
Since which version changed this behavior? And why works this?
The behavior that caught your attention was introduced with the new feature of TextBlocks
This new feature was first introduced in Java 13 and the specific change which is the reason for the behavior that you observe, came with the second preview of TextBlocks which was included in Java 14 Text Blocks Second Preview .
According to Text Blocks Second Preview
the new \s escape sequence simply translates to a single space (\u0020).
The \s escape sequence can be used in both text blocks and traditional string literals.
Also this new feature about TextBlocks was officially included in LTS version of Java 17.