I am working on python to extract certain string between match strings. These strings are generated from a list which is again generated dynamically by a separate python function. The list I am working on looks like this:-
sample_list = ['line1 this line a first line',
'line1 this line is also considered as line one...',
'line1 this line is the first line',
'line2 this line is second line to be included in output',
'line3 this should also be included in output',
'line1 this contain other strings',
'line1 this may contain other strings as well',
'line2 this line is second line to be included in output',
'line3 this should also be included in output',
'line1 what the heck is it...'
]
The output I want is similar to this:-
line1 this line is the first line
line2 this line is second line to be included in output
line3 this should also be included in output
line1 this may contain other strings as well
line2 this line is second line to be included in output
line3 this should also be included in output
As you can see, I want to extract the text/lines which are starting as line1 and ending with line3 (up to line ending). The final output includes both the matching words (ie. line1 and line3).
The code I have tried is:-
# Convert list to string first
list_to_str = '\n'.join(sample_list)
# Get desired output
print(re.findall('\nline1(.*?)\nline2(.*?)\nline3($)', list_to_str, re.DOTALL))
This is what I am getting as an output ():-
[]
Any help is appreciated.
Edit1:- I have done some work and found this nearest solution:-
matches = (re.findall(r"^line1(.*)\nline2(.*)\nline3(.*)$", list_to_str, re.MULTILINE))
for match in matches:
print('\n'.join(match))
It gives me this output:-
this line is the first line
this line is second line to be included in output
this is the third and it should also be included in output
this may contain other strings as well
this line is second line to be included in output...
this is the third should also be included in output
The output is almost correct but it does not include the match text.
.startswith('line1'), or'line2', etc.(<your regex>)Example(^line1(.*)\nline2(.*)\nline3(.*)$)