3

I downloaded files through FTP and it added newlines after each line. Now, in my code I had blank lines added for context. After downloading, each empty line I added is now three empty lines.

So if I remove all blank lines with a regex expression like this ^\n it removes also those lines I added for context. And this ^\n{1} doesn't help either.

This is an example of HTML that I have now after downloading it through FTP:

<nav>

  <ul>

    <li><a href="#">London</a></li>

    <li><a href="#">Paris</a></li>

    <li><a href="#">Tokyo</a></li>

  </ul>

</nav>



<div>new context</div>



<div>new context</div>

This is what I would want it to look like:

<nav>
  <ul>
    <li><a href="#">London</a></li>
    <li><a href="#">Paris</a></li>
    <li><a href="#">Tokyo</a></li>
  </ul>
</nav>



<div>new context</div>



<div>new context</div>

Basically what I need is a regex expression that would find all empty lines that aren't after or before another empty line.

M. Ullman
  • 41
  • 1
  • 8

5 Answers5

3

This works to find a single blank line

Find: (\S[^\S\n]*\n)[^\S\n]*\n(?![^\S\n]*\n)
Replace: $1

https://regex101.com/r/7IbV1Y/1

Explained

 ( \S [^\S\n]* \n )            # (1) A line with a piece of non-wsp text 
 [^\S\n]* \n                   # Single blank line with a line break
 (?! [^\S\n]* \n )             # Not a blank line with a line break ahead
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Ah, now it's working! Didn't think that last update was that important. – M. Ullman Mar 27 '19 at 20:08
  • Works on lines that have spaces on them too. If you want it to work only on lines that don't have spaces, then look at my answer. https://stackoverflow.com/a/55382979/10060799 – M. Ullman Mar 27 '19 at 20:12
2

In vscode when you hit Ctrl + F enable regex and insert the following into the Find field:

>\n\n(?!\n)

and

>\n

into the Replace field. Now select Replace All.

This will lead to the result you described in your original question. I tested it with vscode 1.32.3

HaaLeo
  • 10,065
  • 3
  • 44
  • 55
1

What you might do is match a single line where there is at least a non whitespace char present. Then match the following empty line followed by a newline. That will also take into account if the empty lines contains spaces themselves.

Then use a positive lookahead to assert that on the next line there is a non whitespace character present.

Replace those matches with the first capturing group $1

^([ \t]*\S.*\n)[ \t]*$\n(?=[ \t]*\S)

That will match

  • ^ Start of string
  • ( Capture group
    • [ \t]*\S.*\n Match 0+ times a space or tab, then a non whitespace char followed by matching until the end of the string and a newline
  • ) Close group
  • [ \t]*$\n Match an empty line or only spaces or tabs
  • (?= Positive lookahead, assert what follows
    • [ \t]*\S) Match 0+ times a space or tab followed by a non whitespace char
  • ) Close lookahead

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

Try something like this:

(?<!\n)\n(?!\n$)

with g and m options and replace with an empty string.

For a working example see https://regex101.com/r/uEpQ0L/1

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • how do I use ```g``` and ```m``` options in VS Code? – M. Ullman Mar 27 '19 at 17:17
  • Search the Weg. E.g.https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-options – Valdi_Bo Mar 27 '19 at 17:20
  • I don't know the exact setup for VS Code but `g` means global and `m` means multiline. Here's a resource for multiline, at least: https://stackoverflow.com/questions/41150941/multi-line-regular-expressions-in-visual-studio-code – saritonin Mar 27 '19 at 17:21
  • The negative look behind feature is not supported in all browsers. I don't think it's supported in VS Code, since it is returning an error. – M. Ullman Mar 27 '19 at 17:23
  • @saritonin After research I think it is the default when using ```\n``` in a regex. As seen here for multi line: https://stackoverflow.com/questions/52647894/multiline-regular-expression-search-in-visual-studio-code – M. Ullman Mar 27 '19 at 17:25
  • Check https://code.visualstudio.com/updates/v1_29#_multiline-search I found there description concerning multiline and lookbehind / lookahead. – Valdi_Bo Mar 27 '19 at 17:34
  • Upon looking further I have found that the lookbehind assertion was updated in the last version 1.32. It was missing from the release notes as seen here: https://github.com/Microsoft/vscode/issues/68004 – M. Ullman Mar 27 '19 at 19:35
  • After updating to the latest version I am not getting an invalid error when inserting the above regex in the search field. Only it isn't giving me the desired result. After a little playing around this works for me: `(?<!^\n)^\n(?!\n$)` – M. Ullman Mar 27 '19 at 19:37
0

EDIT:

After seeing Valdi_Bo's answer it seemed like a very good solution. The only problem is, it doesn't work properly in VS Code. So in order for it to work in VS Code, you must be updated to version 1.32 and change the regex to:

(?<!^\n)^\n(?!\n$)

and then I got the desired result as seen here: https://regex101.com/r/b3Jnk2/1

Previous Answer:

This regex did it for me.

^(\n)(?!\n{2}) This is the result I got:

<nav>
    <ul>
        <li><a href="#">London</a></li>
        <li><a href="#">Paris</a></li>
        <li><a href="#">Tokyo</a></li>
    </ul>
</nav>

<div>new context</div>

<div>new context</div>

After you do this and you still have a couple of empty lines that you want to get rid of but not those empty lines that where added for context, use this regex: ^(\n{2})

M. Ullman
  • 41
  • 1
  • 8
  • Can be pretty helpful when downloaded files from ftp get corrupted in such away. Best thing to do is make sure it doesn't happen next time, see - https://superuser.com/questions/1091022/stop-filezilla-from-inserting-blank-lines-in-text-files – M. Ullman Mar 27 '19 at 17:51