Extracting repeated patterns from a text file in python
Question: I am looking to extract all the text between a repeating pattern in a text
file. My text file XYZ.txt looks something like this:

    
    
    Start
    
    This is a great day
    
    End
    
    Start
    This is another great day
    
    End
    
    Start
    This is 3rd great day
    End
    

I am looking extract the all the text between every start and End, my output
should be like:

    
    
    This is a great day
    This is another great day
    This is 3rd great day
    

and I am looking to save all the outputs as separate HTML file. The code that
I am using is as follows:

    
    
    import re
    with open('XYZ.txt') as myfile:
        content = myfile.read()
    
    text = re.search(r'Start\n.*?End', content, re.DOTALL).group()
    
    print(text)
    

But code above only prints the first line. Not sure how I can print all the
values between the pattern and save them as seperate html files. I would
really appreciate any directions.

Thank You


Answer: You need to use `re.findall` to find all occurrences of regex.

    
    
    >>> lines
    'Start\n\nThis is a great day\n\nEnd\n\nStart\nThis is another great day\n\nEnd\n\nStart\nThis is 3rd great day\nEnd\n'
    >>>
    >>> re.findall('This is.*day', lines)
    ['This is a great day', 'This is another great day', 'This is 3rd great day']
    

