JP
/
EN

Regex: Remove blank lines from text

titleRegexRemarks
Blank lines
Newline codes only
^$(\r\n|\r|\n)?Lines with only Newline codes (CR, LF, CRLF).
Blank lines
Newline codes + All Blanks
^\s*?$(\r\n|\r|\n)?Lines with only newline codes and all blanks.
* All Blanks = space, full-width space, tab and new page.
Blank lines
Newline codes + Space + Tab
^[ \t]*?$(\r\n|\r|\n)?Lines with only newline codes, space and tab.
Blank lines
Newline codes + Space
^ *?$(\r\n|\r|\n)?Lines with only newline codes and space.
Blank lines
Newline codes + Tab
^\t*?$(\r\n|\r|\n)?Lines with only newline codes and tab.

This is a regular expression that removes blank lines from text. Newline code only, newline code, space / tab character only, etc.

Newline codes match all CRLF, CR, and LF. If you want to target only a specific line feed code, replace (\r\n|\r|\n) with any line feed code such as \r\n.

Note that the above regular expression assumes multiline mode. Please note that in single-line mode, the concept of lines is lost.

e.g. Remove blank lines using regular expressions

You can remove blank lines by searching for them in the table’s regular expression and replacing them with blank characters.

For example, suppose we have a text like this.

aaaaaaaa\n
\n
␣␣□□\t\t\n
␣␣\t\t\n
␣␣\n
\t\t\n
bbbbbbbb\n

: Half-width space
: Full-width space
\t: Tab
\n: Newline code

Remove Blank lines (Newline codes only)

Regex: ^$(\r\n|\r|\n)?

aaaaaaaa\n
\n				-> Remove
␣␣□□\t\t\n
␣␣\t\t\n
␣␣\n
\t\t\n
bbbbbbbb\n

Remove Blank lines (Newline codes + All Blanks)

Regex: ^\s*?$(\r\n|\r|\n)?

aaaaaaaa\n
\n				-> Remove
␣␣□□\t\t\n	-> Remove
␣␣\t\t\n		-> Remove
␣␣\n			-> Remove
\t\t\n			-> Remove
bbbbbbbb\n

Remove Blank lines (Newline codes + Space + Tab)

Regex: ^[ \t]*?$(\r\n|\r|\n)?

aaaaaaaa\n
\n				-> Remove
␣␣□□\t\t\n
␣␣\t\t\n		-> Remove
␣␣\n			-> Remove
\t\t\n			-> Remove
bbbbbbbb\n

Remove Blank lines (Newline codes + Space)

Regex: ^ *?$(\r\n|\r|\n)?

aaaaaaaa\n
\n				-> Remove
␣␣□□\t\t\n
␣␣\t\t\n
␣␣\n			-> Remove
\t\t\n
bbbbbbbb\n

Remove Blank lines (Newline codes + Tab)

Regex: ^\t*?$(\r\n|\r|\n)?

aaaaaaaa\n
\n				-> Remove
␣␣□□\t\t\n
␣␣\t\t\n
␣␣\n
\t\t\n			-> Remove
bbbbbbbb\n
Hirota Yano / Japan / Programmer
I am publishing a web tool I created as a hobby. It is free of charge, so please feel free to use it.
© Hirota Yano