I teach a Java-based Advanced Computer Science class and we primarily use the Eclipse IDE. I recently had an assignment where students explored regex in Java, and have run into an issue where incorrectly formed escape sequences actually pass in Eclipse, but fail java compilation when running through my test automation. Here is an example of a regex that should fail but does not on eclipse:
in = in.replaceAll(" ([^\s]*([^0-9\\ |\\-|\\/|\\(|\\)|\\*|\\.|\\s] |[^0-9]\\.|\\.[^0-9])[^\s]*) "," Error:$1" separator " ");
When compiling outside of eclipse, this code correctly fails to compile with the following error message:
RegularExpressions.java:60: error: illegal escape character
in = in.replaceAll(" ([^\s]*([^0-9\\ |\\-|\\/|\\(|\\)|\\*|\\.|\\s] |[^0-9]\\.|\\.[^0-9])[^\s]*) "," Error:$1" separator " ");
^
I have been unable to figure out why eclipse ignores this issue. I am assuming that there is some setting somewhere that I need to update, but I have not found it yet. It is particularly frustrating for students when their code "works for them" but then fails my testing.
Any help would be greatly appreciated.
CodePudding user response:
\s
is a valid Java escape for space in Java 15 and later (JLS §3.10.7)
There is a bug in Eclipse see here which accepts \s
when the language level is less than 15. This will be fixed in 2021-12 (4.22)