Home > Software design >  RegExp match more than two empty lines
RegExp match more than two empty lines

Time:11-05

I am using Emacs and one of its modes separates the code into groups of smaller codes if there is an empty line between lines. It uses ^\s-*$ regular expression as a matching line. However, I want to configure it to behave as separate to groups when there is 2 or more empty lines between lines. What is the regular expression for this?

For example right now:

*Group 1 starts*
Line1
Line2
*Group 1 ends*
--empty line
*Group 2 starts*
Line3
*Group 2 ends*
--empty line
--empty line
*Group 3 starts*
Line4
Line5
*Group 3 ends*

I want it to look like this:

*Group 1 starts*
Line1
Line2
--empty line
Line3
*Group 1 ends*
--empty line
--empty line
*Group 2 starts*
Line4
Line5
*Group 2 ends*

CodePudding user response:

IIUC \s-*$ is the expression of your separator. In this case it matches a line starting with a whitespace and followed by any number of dashes - (0 works as well). $ marks the end of the line.
If you want to separate when there are two or mor of such lines, use a quantifier:

`(\s-*$){2,}`

CodePudding user response:

You can use a repeated group, but looking at this page you have to escape the parenthesis and the curly's.

Where according to the documentation, the \s- means:

\s-   whitespace character

For example

\(\s-*$\)\{2,\}
  • Related