I’m grabbing lines from a text file and sifting line by line using regular expressions. I’m trying to search for blank lines, meaning nothing or just whitespace.
However, what exactly is empty space? I know that whitespace is \s but what is a line that is nothing at all? null (\0)? newline (\n)?
I tried the test harness in the Java tutorial to try and test to see what an empty space is but no luck so far.
An empty string
""is a string. It’s notnull. It doesn’t have any character, not even\0(which is just a character in Java, i.e. it’s not a string terminator (JLS 10.9)).The following are all true:
The following are true exclusively for an empty string:
This is also true for an empty string as well as all other strings containing only whitespaces:
This is because
*is zero-or-more repetition of a pattern. Zero repetition of a whitespace is an empty string.The following is also true for all strings containing only whitespaces:
Further discussions
\s*matches zero or more whitespaces, and"test test".matches("\\s*")isfalse.However, you can
find\s*in"test test", just as you can find it in any string, because\s*can match the empty string, and all stringscontains("").[^a-zA-Z0-9\W]doesn’t really make any sense, and in fact"_".matches("^\\s*[^a-zA-Z0-9\\W]|^$").Perhaps the confusion is because
matchesin Java needs to match the whole string (i.e. as if you’ve surrounded the entire pattern with^and$), so you can drop the anchors formatchesbut you’d need it for, sayfind. The proper regex for such methods would then be"^\\s*$", with the anchors explicitly included.The following is an excerpt from cletus’s original answer (which is now deleted):
The
Pattern.MULTILINEallows^and$to also match line terminators withinfileString.