If what you're parsing is a file full of e-mails, I'd recommend going straight for the Mail-Tools.
will break up a file full of mail messages into individual messages.
can be used to parse that header. Once you're dealing with headers individually, your problem becomes much simpler.