Inspectopedia Help

Non-terminal use of '\s' escape sequence

Reports \s escape sequences anywhere except at text-block line endings or within a series of several escaped spaces. Such usages can be confusing or a mistake, especially if the string is interpreted as a regular expression. The \s escape sequence is intended to encode a space at the end of text-block lines where normal spaces are trimmed. In other locations, as well as in regular string or char literals, \s is identical to an ordinary space character (" ").

Example:

if (str.matches("\s+")) {...}

Here it's likely that "\\s+" was intended (to match any whitespace character). If not, using str.matches(" +") would be less confusing.

A quick-fix is provided that replaces \s escapes with space characters.

This inspection depends on the Java feature ''\s' escape sequences' which is available since Java 15.

New in 2022.3

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin:

Java, 241.16690

Last modified: 29 April 2024