H.Birdi
H.Birdi

Reputation: 45

How to get lines after the word match and before other specific word?

I have this string:

Overview

Somebody request a review

Title

Test

Test

Resolution

I would like get all the lines after the word "Overview" and before the Word "Resolution"

I tried this Regex: Overview.*[\r\n]+(.*)

https://regex101.com/r/QibhMq/1

Upvotes: 1

Views: 264

Answers (2)

helb
helb

Reputation: 3234

It might seem like an overkill, but nested repeating group and the actual boundary words should be pretty robust:

Overview[\r\n]((?:.*[\r\n].*)*)Resolution

Demo in JS:

const data = `Something
Overview
Somebody request a review
Title
Test
Test
Resolution
Blah
Something`

const after = "Overview"
const before = "Resolution"

const regex = new RegExp(`${after}[\r\n]((?:.*[\r\n].*)*)${before}`)

const match = data.match(regex)
if (match) console.log(match[1])

enter image description here

Demo and explanation on Regex101: https://regex101.com/r/QibhMq/6

Upvotes: 2

Dalorzo
Dalorzo

Reputation: 20014

For this you need to use the modifiers. I would recommend the use of

/s singleline

Overview.*[\r\n]+(.*)/s

Online Demo

Upvotes: 1

Related Questions