Home > Net >  Match consecutive lines that start with 1 or more spaces
Match consecutive lines that start with 1 or more spaces

Time:08-12

Could anyone offer up assistance to make this work: https://regex101.com/r/s1X84J/1

REGEX

^((?:(?:[ ]{1,}|\t).*(\R|$)) ){1,}

It should match any consecutive lines that start with one or more spaces. In the example, I am able to get it to match the first block of text. I am trying to get it to match the next block of consecutive text starting with one or more spaces as Match 2.

CodePudding user response:

Firstly you need the global flag/option set (/g) to return more than one match.

Secondly the following returns multiple lines starting with space. It uses a look back to ensure the match starts on an even line boundary:

/(^|(?<=\n))( [^\n]*\n\r?)   /gm

The flags are on the right.

CodePudding user response:

You need to use g and m flag with the following pattern:

^\h.*(?:\R\h.*)*

If your real regex flavor does not support \h (horizontal whitespaces) you can use either [^\S\r\n] or [\p{Zs}\t] instead.

Details:

  • ^ - start of a line
  • \h - a horizontal whitespaces
  • .* - the rest of the line
  • (?:\R\h.*)* - any zero or more occurrences of
    • \R - any line break sequence
    • \h - a horizontal whitespaces
    • .* - the rest of the line.

It needs to be adjusted if the regex flavor is not PCRE / Onigmo / Java.

  • Related