我有一个文件里的文本。

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'

我希望有一个表达式来捕获字符串(应跳过以$开头的行):
['.\..\..\FE_10-28\ASSY.bdf', '.\..\..\FE_10-28\standalone\COORD.bdf']

我设法过滤了单行字符串:
    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))

但是,对于多行来说会是怎样的呢?

最佳答案

您可以使用这个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']

10-02 16:05