Search code examples
pythonregexreadlines

When iterating through lines in txt file, how can I capture multiple subsequent lines after a regex triggers?


I have a txt file:

This is the first line of block 1. It is always identifiable
Random
Stuff

This is the first line of block 2. It is always identifiable
Is
Always

This is the first line of block 3. It is always identifiable
In
Here!

I want to iterate through each line and look for the following code to trigger and capture a fixed amount of lines following:

for line in lines:
    match = re.compile(r'(.*)block 2.(.*)'.search(line)
    if match:
        #capture current line and the following 2 lines

After parsing the txt file, I want to return:

This is the first line of block 2
Is
Always

In my particular example, the first line of my block is always identifiable. There is a consistent row count per block. The contents of lines >= 2 will always change and cannot reliably be returned when using regex.


Solution

  • You can call the next() function to get the next element in the iterator.

    def get_block2(lines):
        for line in lines:
            match = re.compile(r'(.*)block 2\n').search(line)
            if match:
                line2 = next(lines)
                line3 = next(lines)
                return line, line2, line3