in reply to Regex question

You could do it from the command line using a combination of grep, head, and tail. Use grep -n to find out the line number that your string is on. Then you can use the others to get all the lines you want, specified by line number.

Otherwise, I'm inclined to keep an array of the lines you might want to remove if you find a match on the next line. When you don't find the match, output the earliest line and insert the unmatching line. When you do find a match, discard the buffered lines, discard the next three lines, and then just "print while (<>)" for the rest of your life.