Parsing multiple lines from text file using regex

Marc marc at
Mon Nov 4 02:28:04 CET 2013

>        This is an alternative solution someone else posted on this list for a similar problem I had:


>        #!/usr/bin/python3

>        from itertools import groupby

>        def get_lines_from_file(file_name):

>        with open(file_name) as reader:

>        for line in reader.readlines():

>        yield(line.strip())


>        counter = 0

>        def key_func(x):

>        if x.strip().startswith("banner") and x.strip().endswith("<banner text delimiter>"):

>        global counter

>        counter += 1

>        return counter


>        for key, group in groupby(get_lines_from_file("my_data"), key_func):

>        print(list(group)[1:-1])




Thanks Jason,


banner = re.compile(r'banner\s+(\w+)\s+(.+)(.*?)\2', re.DOTALL).findall(lines)


worked nicely to get what I needed:


outfile.write("Banner type: %s     Banner Delimiter: %s\n" % (banner[0][0], banner[0][1]))
outfile.write("Banner Text:\n")


Probably not the prettiest, most concise code, but it gets the job done.


Thanks again,



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list