简体   繁体   English

排除匹配的正则表达式模式

[英]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 使用的模式:( (?im)(?:^( (?s:.*?))(?:^[^ ])) /g

See example at https://regex101.com/r/msVC5b/1 参见示例https://regex101.com/r/msVC5b/1

Please can anyone help me? 有人可以帮我吗? I've spent hours and hours searching on SO and trying negative lookarounds ;) 我已经花了几个小时在SO上进行搜索,并尝试使用负面的环顾四周;)

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) Regex101上的演示(改编自您的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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