How to check if a line is blank using regex
The pattern you want is something like this in multiline mode:
- ^ is the beginning of string anchor.
- $ is the end of string anchor.
- \s is the whitespace character class.
- * is zero-or-more repetition of.
In multiline mode, ^ and $ also match the beginning and end of the line.
References:
A non-regex alternative:
You can also check if a given string line is «blank» (i.e. containing only whitespaces) by trim() -ing it, then checking if the resulting string isEmpty() .
In Java, this would be something like this:
The regex solution can also be simplified without anchors (because of how matches is defined in Java) as follows:
API references
- String String.trim()
- Returns a copy of the string, with leading and trailing whitespace omitted.
- Returns true if, and only if, length() is 0 .
- Tells whether or not this (entire) string matches the given regular expression.
@Adnan: take note of Bart’s comment in Marcelo’s answer; depending on how you want to handle multiple blank lines, the pattern may change slightly.
This is incorrect. When the line end is «\r\n» it finds a single line end, not the blank line «\r\n\r\n».
Note that ^\s*$ won’t detect all blank lines in a string, as \s* will also match \n , so runs of blank lines become one match. Whereas ^\s*?$ will match each blank line.
Actually in multiline mode a more correct answer is this:
The accepted answer: ^\s*$ does not match a scenario when the last line is blank (in multiline mode).
Edit: added a ^ toward the beginning to catch the case of lines ending with */ followed by a new line. Thanks John Henry.
Exactly, and I confirmed this is the case. The accepted answer missed many empty lines in my file, but this caught them all. The union of both regexes catches every case.
This answer worked perfectly in a tool such as Notepad++. The accepted answer matched multiple empty lines but not single empty lines.
There is a small fix on top of this in this other answer (if you have */ (end of comment) followed by an empty line).
@Adnan, note that \s also matches line breaks, so you won’t «find» single empty lines inside a string containing successive empty lines.
Full credit to bchr02 for this answer. However, I had to modify it a bit to catch the scenario for lines that have */ (end of comment) followed by an empty line. The regex was matching the non empty line with */ .
All I did is add ^ as second character to signify the start of line.
The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string.
On Windows you also need to consider the carriage return character \r so the regex would be ^[ \t\r\n]*$ . But ^\s*$ is better — more concise. If you don’t want to match newlines, you can use \h (meaning horizontal whitespace) as in ^\h*$
Here Blank mean what you are meaning.
A line contains full of whitespaces or a line contains nothing.
If you want to match a line which contains nothing then use ‘/^$/’.I agree; «blank» lines is ambiguous; it can contain \r, \n, or nothing. In my case, I had to simply use ^$ to select «nothing» lines copied from a LibreOffice Calc spreadsheet column that contained empty cells.
Somehow none of the answers from here worked for me when I had strings which were filled just with spaces and occasionally strings having no content (just the line terminator), so I used this instead:
Well. I tinkered around (using notepadd++) and this is the solution I found
\n for end of line (where you start matching) — the caret would not be of help in my case as the beginning of the row is a string \s takes any space till the next string
user asks for a «simple regex that will check if a line is blank» this regex (tested in regexpal.com) does exactly that. why dont u test it?
using R, our test vector: test_vec
I’m not really sure what you’re doing though, are you doing ctrl+f in notepad++ ? In this case you can find (though not really match) the blank lines by selecting «Extended» Search mode and searching for ‘\n\s’, if you select «Regular Expression’, your string will match the same, and you can also try @polygenelubricants ‘s solution. The latter will really match the line, you can check and see the difference. I would suggest that you edit your answer to be more clear about what you’re advising, so readers can take more value from it.