user2196351
user2196351

Reputation: 567

Regex match lines until the first empty line

Looking for a simple regexp which will match all lines until the first empty line.

I tried the following regexp but it does not handle all cases.

Here is the regexp:

/[\s\S]*(?=\r{2,})/

For example, this case: https://regex101.com/r/lW7IQ8/1

Upvotes: 2

Views: 6465

Answers (2)

Jan
Jan

Reputation: 43169

If it is really PHP (PCRE and the like) you are trying this into, you might very well get along with:

^(?:.+\R)+

in multiline mode, see your modified demo (clue here is to require at least one character, thus not allowing empty lines).


For JavaScript the pattern is similar

^(?:.+[\n\r])+

See a demo for the latter on regex101.com as well.

Upvotes: 4

Shen Yudong
Shen Yudong

Reputation: 1230

^[\s\S]*?(?=\n{2,})

*? lazy match, add ^ for only match the 1th one.

Upvotes: 8

Related Questions