I have a file like this which has multiple lines between a delimiter and i wanted to capture everything in between start_of_compile and end_of_compile excluding the comments.
string i want to parse below text
#####start_of_compile - DO NOT MODIFY THIS LINE#############
##################################################################
parse these lines in between them
...
....
###################################################################
#####end_of_compile -DO NOT MODIFY THIS LINE#################
###################################################################
I want to see match (.*) to match multiple lines between start and end. Currently its not
Instead i see below error
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'
Your regex starts to try matching a starting line that consists of only # that is not present.
Apart from that, you have to use
re.Sinstead ofre.MULTILINEfor your pattern and make the quantifier non greedy to not have a last line with only # chars.If the data always looks like that, you don't have to use the
re.Sand a non greedy quantifier which prevent unnecessary backtracking.regex demo
Example
Output