[prog] Parsing mail headers in Perl

Dan Richter daniel.richter at wimba.com
Mon Jun 30 11:28:24 EST 2003


Parsing single-line mail headers is easy, but multi-line messages present a 
challenge. Here's what I came up with:

if ( $headers =~ /Received: (.*?)\n[^\t ]/ ) { ... }

This works when the Received line has no newlines, but not when it has 
several lines. (In the latter case, there's no match at all.)

Here are some possibilities that I've already explored:
1) continued lines are indicated by spaces, not tabs. There's a space after 
the \t.
2) the [^\t ] is interpreted as "neither '\' nor 't' nor space". In that 
case, it should match a single line.
3) the .*? causes the match to quit when it reaches the first newline (and 
the match fails because of the following tab). Maybe, but I thought that 
".*?" would always return a match if ".*" did, and the only thing that 
would differ was WHICH match. Can anyone confirm this?

Thanks.

--
    It isn't a mistake to have strong views. The mistake is to have
    nothing else.
           - Andy Warhol, _The Philosophy of Andy Warhol_



More information about the Programming mailing list