简体   繁体   中英

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?

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","") .

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM