Your original prints out the whole thing because, in your match statement, you print the whole of the matching line. Since the whole document is put into one line, the whole thing prints.
In my program, I only print out what matches that particular regex, if and only if anything matches. It won't ever print out anything more than what's specified.