Regex in Notepad ++ to remove blank lines

I have several html files, and some of them have several empty lines, I need a regular expression to remove all empty lines and leave only one empty line. Thus, it deletes something more than one empty line, and leave those that are just one or none (none of them have text in them).

I also need to consider lines that are not completely empty, as some lines may have spaces or tabs (characters that do not appear), so I need them to consider these lines with a regular expression that needs to be removed while there are more than one strings.

+4
source share
7 answers

Search

^([ \t]*)\r?\n\s+$ 

and replace with

 \1 

Explanation:

 ^ # Start of line ([ \t]*) # Match any number of spaces or tabs, capture them in group 1 \r?\n # Match one linebreak \s+ # Match any following whitespace $ # until the last possible end of line. 

\1 will contain the first line of space characters, so when you use this as a replacement string, only the first line of spaces will be saved (excluding the line at the end).

+8
source

This worked for me on notepad++ v6.5.1. UNICODE windows 7 notepad++ v6.5.1. UNICODE windows 7

Search: ^[ \t]*\r\n

Replace: nothing, leave blank

Search Mode: Regular expression.

+9
source

find (\r?\n(\t| )*){3,} , replace it with \r\n\r\n , check the box next to "Regular expression" and ". matches the new line".

Tested with Notepad ++ 6.2

+2
source

This will replace consecutive empty lines containing spaces (or not) and replace them with one new line.

Search (\s*\r?\n){3,}

replace \r\n

+2
source

enter image description here

You can find yourself what you need to replace \ n \ n OR \ n \ r \ n or \ r \ n \ r \ n, etc. Now you can even change your regex ^([ \t]*)\r?\n\s+$ to suit your needs.

+2
source

I tested any of the above suggestions, it was always either too little or a lot removed. So either you didn’t get an empty line where at least one was previously or not removed enough (spaces remained, etc.). Sorry, I can not write comments. Tested with both 6.1.5 and updated to 6.2 and tested again. depending on how mayn files are, I would suggest using

 Edit->Blank Operations->Trim trailing whitespace 

This is followed by Ctrl + A and

 TextFX -> TextFX Edit -> Delete surplus blank lines 

The macro I tried to record did not work. Theres even a macro to easily remove trailing spaces ( Alt + Shift + S , see settings | Shortcut Mapper ... | Macros). There

 Edit->Blank Operations->Remove unnecessary EOL and whitespace 

but it deletes each EOL and puts everything on one line.

+1
source

I searched ^\r\n and clicked "Replace All" with nothing (empty) in the "Replace With" text box.

0
source

Source: https://habr.com/ru/post/1440547/


All Articles