Tom
Tom

Reputation: 13

Match line only if next line is an empty Line

I'm very new to regex, what I'm trying to do is to match a line only if the next line is an empty line.

For example:

  1. This is some text
  2. ( empty line )
  3. This is some text
  4. This is some text
  5. This is some text
  6. ( empty line )
  7. This is some text
  8. ( empty line )

In the previous example, I would like to be able to select only line 1,5,7.

Is this possible with regex in notepad++?

Thanks in advance.

Upvotes: 1

Views: 523

Answers (3)

Avinash Raj
Avinash Raj

Reputation: 174696

You could try the below positive lookahead based regex.

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

\S matches any non-space character. So .*?\S.* matches the line which has at-least one non-space character and the following (?=\n[ \t]*$) asserts that the match must be followed by a newline character and then followed by zero or more space or tab characters.

OR

^.*?\S.*(?=\n\n)

If you mean empty line as line which don't have any single space or tab characters, then you could use the above regex. (?=\n\n) asserts that the match must be followed by a blank line.

DEMO 1

DEMO 2

Upvotes: 1

apgp88
apgp88

Reputation: 985

You can use this regex,

(.*)\n\s*\n

and replace with

\1

Working Demo

It uses a concept of group capture, so here you can use \1 to use captured group, that is line before newline

Upvotes: 2

Alex Smith
Alex Smith

Reputation: 364

This should do the trick:

/(.)*\n\n/

If you're looking for an easy way to test / verify regex rubular is pretty good.

Upvotes: 0

Related Questions