0

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 ;)

3
  • By "all lines preceeding with a space" do you mean \n\s+ or \s+\n ? Commented Oct 17, 2016 at 23:45
  • /\A(.*?)(?=^(?! ))/sm Commented Oct 17, 2016 at 23:53
  • /\A((?: [^\n]*\n)*)/ Commented Oct 17, 2016 at 23:56

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.