Kex
Kex

Reputation: 8589

Regex matching between lines

I have the following text block:

## 8.6.0
- **Upload date:** November 19, 2020
- **Release date:** TBC
- **Internal version:** 1171

### Feature
- dsfdsfds
- sdfdsf
- dsfdsf

### Bug fixes
- sdfsaf
- sdfsad
- sdfsdfdsf

### Internal
- sadfsdfsda
- fsdfgsadfasd
- sdfsda

## 8.5.1
- **Upload date:** November 09, 2020
- **Release date:** November 12, 2020
- **Internal version:** 1170

I would like to extract just the first entry e.g. all the text from the start of the first character in ## 8.6.0 up to just before the first character of ## 8.5.1.

I have tried the following expression:

[#].*[0-9])(.*?)([#].*[0-9])

But it doesn't return the right result. How would I write this expression?

Upvotes: 2

Views: 128

Answers (2)

Peritract
Peritract

Reputation: 769

If I've understood the problem correctly, then

^## (\d+\.?){3}.+?(?=## \d)

should work. Here's a demo.

The pattern does the following:

  • Looks for two hashes followed by a version number, right at the start of the string - ^## (\d+\.?){3}.
  • Looks ahead for another two hashes followed by a number (start of the next set of notes) - (?=## \d)
  • Grabs all the characters in between the two, aiming for as few characters as possible - .+?

To make this work, you need the dotall flag enabled, so . can match newline characters.

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

Use

^##(?!#).*(?:\n(?!##(?!#)).*)*

See the regex demo.

Details

  • ^ - start of a string (if you use it in an environment with the multiline flag enabled by default, try prepending it with (?-m) or use ^(?<![\s\S]))
  • ##(?!#) - a ## substring not followed with another #
  • .* - the rest of the line
  • (?:\n(?!##).*)* - zero or more lines not starting with ## not followed with another #.

Upvotes: 2

Related Questions