1

I tried to detect duplicate blank lines with

\s*\n\n+

https://regex101.com/r/v0imUZ/1

but doesn't seem to work with

  test1
   
    
   
   test2
   test3
  test4

  test1
   
    
   
   test2
   test3
  test4
4
  • 1
    Define "doesn't work." Keep in mind that \s* will also match newline characters. Commented Oct 14, 2022 at 11:04
  • @Rajesh /\n{2,}/ matches 2 or more line breaks. OP needs to match two or more blank lines. /\n[^\S]+/ matches a newline and then one or more whitespace chars (not necessarily a newline). Basically, neither is the solution. Commented Oct 14, 2022 at 11:09
  • @WiktorStribiżew consecutive new lines would mean blank lines. Correct? Commented Oct 14, 2022 at 11:10
  • No, those are empty lines, not blank lines, see the regex demo link. Commented Oct 14, 2022 at 11:10

2 Answers 2

3

As commented,

A simpler way would be /\n{2,}/ or /\n[^\S]+/

you can try /\n[^\S]*\n/.

Idea is to check for new line, optionally followed by whitespace character followed by a new line.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use

\n(?:[^\S\n]*\n)+

See the regex demo. If there can be CRLF endings:

\r?\n(?:[^\S\n\r]*\r?\n)+

Details:

  • \r? - an optional carriage return symbol
  • \n - a newline char
  • (?:[^\S\n]*\n)+ - one or more occurrences of
    • [^\S\n]* - zero or more whitespace chars excluding newline char, and then
    • \n - a newline char.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.