Regex: Many lines with same word, grab until line with 1st occurrence

Hi

I have different section (Sales, Revenue, etc) in my text. Each section will have the same people names.

Sales
Peter $235
John $300

Revenue
Peter $450
John $400

For say, Sales section, I want to grab until the end of the line with the last person name.

Sales
Peter $235
John $300

I tried this
(System.Text.RegularExpressions.Regex.Match(Text,"(?<=Sales.)((\n).+)(?<=John)").Value)

but it will grab until the last occurrence of John, and it stops at “John”

Thank you

I tried this

(System.Text.RegularExpressions.Regex.Match(Text,"(?<=Sales. )((\n).+)""\n.?John.").Value)

but it also grab until the last John

@Anonymous2 Do you know the names of the People in the Sections before hand ?

Yes. I know the names. I just want to grab until the 1st occurrence of the name
I used lazy operator (?) but it doesn’t seem to work too. Why should I place the lazy operator?

@Anonymous2 Wait. I’m sorry the question should actually be , Do you know the Section names before hand? :sweat_smile:

Hi

The order of each section will change. So it is useless to use the Section names as back delimiter.

We need to use the last person name in the section

Thank you

1 Like

@Anonymous2 - have a look on below article - it will help you for sure! having similar example of yours.

[quote=“Anonymous2, post:2, topic:240285”]
(System.Text.RegularExpressions.Regex.Match(Text,"(?<=Sales. )((\n).+)" "\n. ?John. ").Value)

Hi GBK & Others,

I have tried different variations of the REGEX but it just doesn’t work. Why should I place the lazy operator?

Will appreciate if someone can help

Thank you

@Anonymous2 - can you share your actual string and expected results + assumptions if any.

1 Like

Hi

Due to confidentiality, I can’t share the exact string. But the main idea is there. I just want to match until the line with the 1st John. Thank you!

Sales
Peter $235
John $300

Revenue
Peter $450
John $400

Are there other names ? Will the first name be Peter always and the Last Name be JOhn always ? Because as you said if the Sections aren’t properly known that means We cannot use Sales as Well in the regex to determine the values

@Anonymous2 Check this link :

1 Like