How to check if a line is blank using regex

Regex

Regex Problem Overview


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

Case;

"    some"   // not blank
"   " //blank
"" // blank

Regex Solutions


Solution 1 - Regex

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

Solution 2 - Regex

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

Solution 3 - Regex

Try this:

^\s*$

Solution 4 - Regex

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.

Solution 5 - Regex

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.

Solution 6 - Regex

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

Solution 7 - Regex

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

\n\s*

Solution 8 - Regex

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdnanView Question on Stackoverflow
Solution 1 - RegexpolygenelubricantsView Answer on Stackoverflow
Solution 2 - Regexbchr02View Answer on Stackoverflow
Solution 3 - RegexMarcelo CantosView Answer on Stackoverflow
Solution 4 - RegexJohn HenryView Answer on Stackoverflow
Solution 5 - RegexsoulmergeView Answer on Stackoverflow
Solution 6 - RegexkiruthikaView Answer on Stackoverflow
Solution 7 - RegexJust MeView Answer on Stackoverflow
Solution 8 - RegexM_TRONICView Answer on Stackoverflow