196

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
  • 17,742
  • 16
  • 75
  • 90
Adnan
  • 1,963
  • 2
  • 12
  • 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

8 Answers8

418

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 Labs LLC
  • 176
  • 3
  • 17
polygenelubricants
  • 364,035
  • 124
  • 554
  • 617
75

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).

bchr02
  • 1,041
  • 8
  • 5
14

Try this:

^\s*$
Marcelo Cantos
  • 174,413
  • 38
  • 319
  • 360
  • 5
    @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
9

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
  • 133
  • 3
  • 9
  • Why not comment on bchr02's answer and suggest the improvement to his answer? – adamlogan Jun 21 '18 at 08:23
  • 1
    @adamlogan yeah, from memory, I think I wanted to do it at the time, but I didn't have enough reputation to comment on someone else's post, so had to do it like this. – John Henry Jun 27 '18 at 01:41
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
  • 71,140
  • 18
  • 117
  • 149
  • 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,037
  • 7
  • 26
  • 32
-1

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
  • 35
  • 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
  • using R, our test vector: `test_vec – moodymudskipper Aug 31 '17 at 10:36
  • like I said i used this on notepad++ and it worked. yours didn't . so i guess we can agree to disagree! – M_TRONIC Aug 31 '17 at 11:01
  • 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
-1

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

\n\s*
Just Me
  • 613
  • 2
  • 12
  • 25
  • 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
  • This didn't work for me. It also finds end-of-lines. – Gabriel Jun 01 '22 at 19:36