Adam
Adam

Reputation: 71

Python regex match multiline text

I have a text in a file.

INCLUDE '.\..\..\
FE_10-28\
ASSY.bdf'

INCLUDE '.\..\..\FE_10-28\standalone\COORD.bdf'

$ INCLUDE '.\..\..\FE_10-28\standalone\bracket.bdf'

$ INCLUDE '.\..\..\
$ FE_10-28\standalone\
$ ITFC.bdf'

I would like to have an expression to capture strings (lines beginning with $ should be skipped):

['.\..\..\FE_10-28\ASSY.bdf', '.\..\..\FE_10-28\standalone\COORD.bdf']

I managed to filter single line string:

    with open(bdf_name,'r') as f:
        file_buff = f.readlines()

    text = ''.join(file_buff)
    regex_incl = re.compile("[^$]\s+include\s+\'(.*)\'",re.IGNORECASE|re.MULTILINE)
    print(regex_incl.findall(text))

But, how would it be for the multiline?

Upvotes: 3

Views: 104

Answers (2)

DYZ
DYZ

Reputation: 57033

In the first place, you need the flag re.DOTALL, otherwise a dot . does not match newlines. And read all the data at once.

with open(bdf_name, 'r') as f:
    data = r.read()

re.findall("^include\s+\'(.*?)\'", data, 
           flags=re.IGNORECASE|re.MULTILINE|re.DOTALL)
#['.\\..\\..\\\nFE_10-28\\\nASSY.bdf', '.\\..\\..\\FE_10-28\\standalone\\COORD.bdf']

If you do not want the line breaks, remove them with .replace("\n","").

Upvotes: 2

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

You can use this regex:

>>> raw = '''
... INCLUDE '.\..\..\
FE_10-28\
ASSY.bdf'

INCLUDE '.\..\..\FE_10-28\standalone\COORD.bdf'

$ INCLUDE '.\..\..\FE_10-28\standalone\bracket.bdf'

$ INCLUDE '.\..\..\
$ FE_10-28\standalone\
$ ITFC.bdf'... ... ... ... ... ... ... ... ... ...
... '''
>>>
>>> re.findall(r"^INCLUDE\s+'(.+?)'\n", raw, re.M|re.DOTALL)
['.\\..\\..FE_10-28ASSY.bdf', '.\\..\\..\\FE_10-28\\standalone\\COORD.bdf']

Upvotes: 2

Related Questions