What if you find a string in a file, match the first line that begins with another string on the line on which it is located, and output it?

is what I want to do if I find a string from a file, match the first line starting with another string on its line direction and output it. Python.

Jan.17,2022

import re
with open("./text.txt") as f:
    lines = f.readlines()
    for index, line in enumerate(lines):
        if re.search("c",line):
            for content in lines[:index][::-1]:
                if re.search("\d",content):
                    print(content)
                    break

Menu