mikey186
mikey186

Reputation: 175

Match everything before a group of dashes

I made a multiline string that looks like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.


----------
User 1 said the following:

Magna fermentum iaculis eu non diam phasellus vestibulum lorem. Lectus quam id leo in vitae turpis massa sed elementum. Ut consequat semper viverra nam libero. Libero justo laoreet sit amet cursus sit amet.


----------
User 2 said the following:

Magna fermentum iaculis eu non diam phasellus vestibulum lorem. Lectus quam id leo in vitae turpis massa sed elementum. Ut consequat semper viverra nam libero. Libero justo laoreet sit amet cursus sit amet.

What I want for regex to do is to match everything before the first set of dashes ----------....10 dashes/hyphens to be exact.

I tried this particular regex to match to no avail.

[^-](-----){10}

Then I tried -{10} as a clue, but it only gives me the 10 dashes/hyphens itself. I want everything before the first set of hyphens/dashes.

Upvotes: 0

Views: 75

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

You could also use a capture group without a global flag to match for only a single match, and match all lines that do not start with 10 hyphens using a negative lookahead.

^(?:(?!----------).*\n)+(?=----------$)

regex demo

Or you can match as least as possible lines, until you encounter a line that starts with 10 hyphens.

^(?:.*\n)+?(?=----------$)

Regex demo

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

You may try matching on the following pattern, with DOT ALL mode enabled:

^.*?(?=----------|$)

Demo

This will match all content up to, but including, the first set of dashes. Note that for inputs not having any dash separators, it would return all content.

If your regex engine does not support DOT ALL mode, then use this version:

^[\S\s]*?(?=----------|$)

Upvotes: 1

Related Questions