Python回复:我想捕获字符串分隔符之间的多行

问题描述 投票:0回答:1

我有一个像这样的文件,它在分隔符之间有多行,我想捕获 start_of_compile 和 end_of_compile 之间的所有内容(不包括注释)。

我想解析下面文本的字符串

#####start_of_compile - DO NOT MODIFY THIS LINE#############
##################################################################

parse these lines in between them
...
....

###################################################################
#####end_of_compile -DO NOT MODIFY THIS LINE#################
###################################################################

我想看到匹配(.*)来匹配开始和结束之间的多行。目前还没有

相反,我看到下面的错误

def checkcompileqel():
    compiledelimiters = ['start_of_compile_setup']
    with open("compile.qel", "r") as compile_fh:
        lines = compile_fh.read()
        matchstart = re.compile(r'^#+\n#+start.*#+\n#+(.*)#+\n#+end.*#+\n#+',re.MULTILINE)
        print(matchstart.match(lines).group(1))
Traceback (most recent call last):
  File "/process_tools/testcode.py", line 25, in <module>
    print(checkcompileqel())
  File "/home/process_tools/testcode.py", line 10, in checkcompileqel
    print(matchstart.match(lines).group(0))
AttributeError: 'NoneType' object has no attribute 'group'
python regex python-re text-parsing
1个回答
0
投票

如果你想匹配之间的线:

\bstart_of_compile\b.*\n#+\n\s*^(.+(?:\n(?!#+$).*)*)

正则表达式演示

© www.soinside.com 2019 - 2024. All rights reserved.