M. Ullman
M. Ullman

Reputation: 41

How can I replace in Visual Studio Code every one newline in a document, but not every two newlines (or more)?

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.

Upvotes: 3

Views: 4157

Answers (5)

user557597
user557597

Reputation:

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

Upvotes: 3

M. Ullman
M. Ullman

Reputation: 41

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

Upvotes: 0

HaaLeo
HaaLeo

Reputation: 11772

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

Upvotes: 2

The fourth bird
The fourth bird

Reputation: 163642

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

Upvotes: 1

Valdi_Bo
Valdi_Bo

Reputation: 31011

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

Upvotes: 1

Related Questions