简体   繁体   中英

Exclude matched regex pattern

I want to match all consecutive lines, prefixed with a space until a line starts without a space!

The problem is that the "end pattern" [^ ] is part of the match. The end pattern is a start-of-line not starting with a space.

The used pattern: (?im)(?:^( (?s:.*?))(?:^[^ ])) /g

See example at https://regex101.com/r/msVC5b/1

Please can anyone help me? I've spent hours and hours searching on SO and trying negative lookarounds ;)

If I've interpreted your request right, you're overthinking it. The pattern you want is this:

/(?:^ .+\n)+/gm

What it'll do is match every line that starts with a space and ends with a newline, one or more times, in a contiguous fashion.

Demo on Regex101 (adapted from yours)

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