julien nascimento
julien nascimento

Reputation: 150

Optional regex group in Python failing

I'm trying to do get some text between keywords into a text using regex. For example:

Text content:

Information Location
Porto Alegre
data data data data data data data data 
Geolocation

If I use this regex (?:Information\sLocation\n.*\n)([\W\w]+)(?:Geolocation), I will get data data data .... It's fine! It's ok!

But, sometimes the text structure can be like this:

Information Location
Porto Alegre
data data data data data data data data 

and my Regex fails! I've tried to find some way to put the last group (?:Geolocation) as optional , but I can't find a solution.

Upvotes: 1

Views: 47

Answers (1)

dawg
dawg

Reputation: 104111

  1. make the capture group less greedy with the addition of ?
  2. put an alternative of\s*\Z for end of file inside last group

Like so:

(?:Information\sLocation\n.*\n)([\W\w]+?)(?:Geolocation|\s*\Z)

Demo

Upvotes: 1

Related Questions