Luke Griffith
Luke Griffith

Reputation: 226

Regex match new line until

Struggling with this one.... This is my data, I want to select all from the first data-group until the next

Original

data-group object name
1234
5677
7890
12356
data-group object name2

Desired Output

data-group object name
1234
5677
7890
12356

I'm currently looking into positive look ahead, and have come up with the following expression

(?=data-group)data-group

This selects.

data-group
data-group

How do I get it to match anything between on the new line.

Upvotes: 2

Views: 1133

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

You need to use DOTAll modifier (?s) inorder to make dot to match line breaks.

(?s)\bdata-group\b.*?(?=\bdata-group\b)

DEMO

OR

This won't print an extra newline character at the last.

(?s)\bdata-group\b.*?(?=\ndata-group\b)

Use [\s\S]*? instead of .*? if your lang won't support s modifier. [\s\S]*? matches any space or non-space character zero or more times non-greedily.

\bdata-group\b[\s\S]*?(?=\ndata-group\b)

DEMO

Upvotes: 1

Related Questions