Diego Henrique
Diego Henrique

Reputation: 157

Regex get text part between two words

I need get all text between Lorem until the last occurrence of CEP code. I'm just getting the first occurrence, but some paragraphs have two CEP codes.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
proident, sunt in culpa qui officia deserunt CEP 142802/AA, mollit anim id est laborum CEP 13342802/AA.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
proident, sunt in culpa qui officia deserunt CEP 11123/AA

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
proident, sunt in culpa qui officia deserunt CEP 2223/AA

Se example working here: https://regex101.com/r/eZ0yP4/2

Thanks!

Upvotes: 1

Views: 140

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

Use positive lookahead assertion like below.

(?s)Lorem.*?CEP \d+\/[A-B]{2}(?=\.?(?:\n\n|$))

DEMO

OR

(?s)Lorem(?:(?!\n\n).)*CEP \d+\/[A-B]{2}

(?:(?!\n\n).)* matches any character but not of \n\n zero or more times. \n\n represents a blank line. (?s) DOTALL modifier which makes dot in your regex to match even line breaks.

DEMO

Upvotes: 2

vks
vks

Reputation: 67968

Lorem.*CEP \d+\/[AB]{2}

Try this.See demo.

https://regex101.com/r/eZ0yP4/3

Upvotes: 0

Related Questions