简体   繁体   中英

Regex how to match indented blocks

I would like to match any block of indentation up to the next line with 0 whitespace. What I tried is this: ^[^\s](\w*|\s+) but this doesn't do what I need. My expectation is to math in a string like:

ajk
 sjdklj
fkldf fl kas dl  4jkl
   asdjk l    sdklj a wk
        ssdklj
4ksk adlj 2jklj

to have groups of matches:

Match 1

ajk
 sjdklj

Match 2

fkldf fl kas dl  4jkl
   asdjk l    sdklj a wk
        ssdklj

Match 3

4ksk adlj 2jklj

So in plain english: any block which starts with 0 whitespace (whitespaces and indentations are allowed inside the blocks). Any helps are appreciated.

Edit This ^[^\s]\w+.* matches first lines of indented blocks, but not the indented lines themselves.

You can try:

/(^\H.*(?:\n^\h+.*)*)/gm

Demo

Or:

/(^\S.*(?:\n^\h+.*)*)/gm

If you don't want to capture leading \n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM