One way to test for empty lines is to use the regex "^$"
, which can be extremely handy when filtering out empty lines from collections
of Strings, for instance. With regard to this, the Javadoc for Pattern (Line Terminators) states the
following:
By default, the regular expressions ^ and
$
ignore line terminators and only match at the beginning and the end, respectively, of the entire input sequence. IfMULTILINE
mode is activated then ^ matches at the beginning of input and after any line terminator except at the end of input. When inMULTILINE
mode$
matches just before a line terminator or the end of the input sequence.
As emphasized, ^ is not going to match at the end of an input, and the end of the input is necessarily included in the empty string, which might lead to completely missing empty lines, while it would be the initial reason for using such regex.
Therefore, when searching for empty lines using a multi-line regular expression, you should also check whether the string is empty.
This rule is raising an issue every time a pattern that can match the empty string is used with MULTILINE
flag and without calling
isEmpty()
on the string.
static final Pattern p = Pattern.compile("^$", Pattern.MULTILINE); // Noncompliant // Alternatively static final Pattern p = Pattern.compile("(?m)^$"); // Noncompliant boolean containsEmptyLines(String str) { return p.matcher(str).find(); } // ... System.out.println(containsEmptyLines("a\n\nb")); // correctly prints 'true' System.out.println(containsEmptyLines("")); // incorrectly prints 'false'
static final Pattern p = Pattern.compile("^$", Pattern.MULTILINE); boolean containsEmptyLines(String str) { return p.matcher(str).find() || str.isEmpty(); } // ... System.out.println(containsEmptyLines("a\n\nb")); // correctly prints 'true' System.out.println(containsEmptyLines("")); // also correctly prints 'true'