[Tutor] Regular Expressions and RFC 822
Sean 'Shaleh' Perry
shalehperry@attbi.com
Mon, 20 May 2002 02:04:51 -0700 (PDT)
>>
>> So even if you used a regex to match re.compile(r'(\w+):(.+)') you
>> would still
>> miss the continuation line. So you almost need a preprocessor which
>> concatenates lines if there is a continuation and when the line is
>> finished
>> parse it. Look at the rfc822 module for a better approach and to avoid
>> reinventing the wheel.
>
> For the record, I really like regular expressions, though I suppose that
> some people don't. Perl-compatible regular expression engines can
> handle the above situation (and many others) but I imagine it uses some
> sort of preprocessor. Python's regex implementation is a little
> different than I'm used to, but I still like 'em (especially in my text
> editor where I use them all the time to modify my code).
>
while (<>) {
if (m/(\w+):(.+)/) {
$type = $1;
$data = $2;
}
}
that code will not handle continuation lines any better than the above python
solution. Which is the style of code we are discussing. A common perl idiom
is to slurp a lot of text into a buffer and walk the buffer with a regex. That
solves the continuation problem for you if you modify your regex to deal with
it.