220

I am trying to make simple regex that will check if a line is blank or not.

Case;

"    some"   // not blank
"   " //blank
"" // blank
Lior Elrom
  • 19,660
  • 16
  • 80
  • 92
Adnan
  • 2,203
  • 2
  • 14
  • 4
  • 1
    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 '/^$/' – Badri Gs Aug 04 '17 at 05:05

10 Answers10

475

The pattern you want is something like this in multiline mode:

^\s*$

Explanation:

  • ^ 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:

if (line.trim().isEmpty()) {
    // line is "blank"
}

The regex solution can also be simplified without anchors (because of how matches is defined in Java) as follows:

if (line.matches("\\s*")) {
    // line is "blank"
}

API references

Elliot Huffman
  • 172
  • 3
  • 18
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • 1
    @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. – polygenelubricants Jun 10 '10 at 08:37
  • This is incorrect. When the line end is "\r\n" it finds a single line end, not the blank line "\r\n\r\n". – mirik Mar 22 '21 at 18:40
  • 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. – JaffaTheCake Aug 01 '22 at 10:07
88

Actually in multiline mode a more correct answer is this:

/(^(\r\n|\n|\r)$)|(^(\r\n|\n|\r))|^\s*$/gm

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.

Bill Christo
  • 1,223
  • 9
  • 8
  • 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. – elmor Oct 05 '17 at 00:16
  • 1
    This answer worked perfectly in a tool such as Notepad++. The accepted answer matched multiple empty lines but not single empty lines. – james Nov 02 '17 at 17:54
  • 1
    The accepted answer did work for my case, multilines. This does. – Robert Gabriel Mar 23 '18 at 11:02
  • the chosed one is not for delete empty lines. this does. so for me this one is best as an answer. – Zen Of Kursat Dec 06 '19 at 08:53
  • There is a small fix on top of this in [this other answer](https://stackoverflow.com/a/46660616/11154841) (if you have `*/` (end of comment) followed by an empty line). – questionto42 Jan 30 '22 at 15:11
15

Try this:

^\s*$
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 6
    @Adnan, note that `\s` also matches line breaks, so you won't "find" single empty lines inside a string containing successive empty lines. – Bart Kiers Jun 10 '10 at 08:34
14

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 */.

New: (^(\r\n|\n|\r)$)|(^(\r\n|\n|\r))|^\s*$/gm

All I did is add ^ as second character to signify the start of line.

John Henry
  • 183
  • 3
  • 10
6

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.

soulmerge
  • 73,842
  • 19
  • 118
  • 155
  • I'd at least change the single space with the class `[ \t]` – Bart Kiers Jun 10 '10 at 08:35
  • 1
    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*$` – ps.pf Sep 07 '15 at 06:03
3

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 '/^$/'.

kiruthika
  • 2,155
  • 7
  • 26
  • 33
  • 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. – JAT86 Jan 16 '23 at 22:01
0

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:

if (str.trim().isEmpty()) {
    doSomethingWhenWhiteSpace();
}
Valerij Dobler
  • 1,848
  • 15
  • 25
-1

My use case was to replace the empty strings specifically in the Intellij-based IDE (Android Studio). For that one, this solution worked perfectly fine:

  1. start replace ( command R )
  2. select Regex replace
  3. search for: ^\s*^
  4. replace with: ( leave it empty )
  5. click replace all.

explanation:

  • ^ ( carrot / shift 6 in regex == start of line )
  • \s* ( back slash s asterisk == any number of white space )
  • ^\s*^ == start of line followed by any number of white space followed by start of line
  • ^$ (carrot followed by dollar sign ) == line with nothing
vchornenyi
  • 336
  • 5
  • 17
-2

Well...I tinkered around (using notepadd++) and this is the solution I found

\n\s

\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

hope it helps

M_TRONIC
  • 33
  • 2
  • OP wants a regex answer, which was given, and is not about new line characters. – moodymudskipper Aug 31 '17 at 08:54
  • 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? – M_TRONIC Aug 31 '17 at 09:53
  • 1
    using R, our test vector: `test_vec <- c(" some"," ","")` . your solution : `grepl("\\n\\s",test_vec) # [1] FALSE FALSE FALSE` , the voted solution: `grepl("^\\s*$",test_vec) # [1] FALSE TRUE TRUE` . the voted solution gives the expected result, yours doesn't. – moodymudskipper Aug 31 '17 at 10:36
  • 2
    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. – moodymudskipper Aug 31 '17 at 11:15
-2

This regex will delete all empty spaces (blank) and empty lines and empty tabs from file

\n\s*
Just Me
  • 864
  • 2
  • 18
  • 28
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the [help center](https://stackoverflow.com/help/how-to-answer). – Tyler2P Apr 04 '22 at 17:05
  • 1
    This didn't work for me. It also finds end-of-lines. – Gabriel Jun 01 '22 at 19:36