in reply to regular expression questions (from someone without experience)

What I generally try to do is to subdivide the problem into four concerns:

  1. Identifying particular records, preferably based on their characteristics, and
  2. Grabbing whatever I need out of each one, writing appropriate logic for each one.
  3. Recognizing when I have found a complete record and need to do something with it... including the last one.
  4. Coding defensively ... arranging for the program, itself, to check its own work.   (Realistically, nothing else can...)

This is the general approach taken by Perl’s little-brother, awk.   (Still a very good tool...)

There really isn’t much more to be said, I’m afraid.   Regular expressions are a black art.   However, there are a great many “Perl-compatible regular expression checker” web sites to help you with debugging.