Željko Krnjić Željko Krnjić - 28 days ago 8
Python Question

Python regex - finding all substrings between two delimiters

I've been dealing with this problem for over a day already and i just can't figure it out..

The problem i have is following:
Given the text:


Obratite pažnju na sljedece:

Pad prometa

Rentabilnost imovine

Neto maržu


**************************************************************


I need to extract all the text that is between word "sljedece:" ( without qouatiton marks) and the row of asterisks.

I tried to use the following code:

import re

text = """
Obratite pažnju na sljedece:
Pad prometa
Rentabilnost imovine
Neto maržu

**************************************************************
"""
pattern = r"sljecece:(.*?)\*+"
napomene = re.findall(pattern, text)

print(napomene)


But it prints out an empty list.

Thx to everyone in advance!

Answer

You have to pass re.DOTALL to make . match newlines:

re.findall(pattern, text, re.DOTALL)

You also have a typo on your pattern r"sljecece:(.*?)\*+" should be r"sljedece:(.*?)\*+".

Comments