Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which regular expression do I have to implement to extract text between two lines containing a string and an arbitrary number of digits?

That’s the code I have:

text = 'LIBRO 1\ndsfsdf\nasdas\nfgfghf\nLIBRO 21\nhghj\nghjhjk\nghjhk\nLIBRO 333'

result = re.findall(r'(?<=LIBRO \d+\n)(.*?)(?=\nLIBRO)', text, re.DOTALL)
print(result)

and this is the error I get:

re.error: look-behind requires fixed-width pattern

the desired result is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

['dsfsdf\nasdas\nfgfghf', 'nhghj\nghjhjk\nghjhk']

>Solution :

You could use split instead of findall, removing the empty entries in the results, as there would be a result for what comes before the first LIBRO:

result = [s.strip() for s in re.split(r'(?m)^LIBRO \d+$', text) if s]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading