re.sub problem

Tim Hicks tim.hicks at iname.com
Thu Nov 2 19:59:26 EST 2000


I have a series of lines that are received from an IMAP server that I would
like to trim down. There are two examples of these below.

24 ('24 (BODY[HEADER.FIELDS (FROM SUBJECT)] {56}', 'From:
tele at telegraph.co.uk\015\012Subject: TPLO Sport Talk\015\012\015\012')
41 ('41 (BODY[HEADER.FIELDS (FROM SUBJECT)] {88}', 'From: The Mutual
<mobilesatprizescratch.com at traq.it>\015\012Subject: Great WAP Phone
Offer\015\012\015\012')

I want to remove everything up to the " 'From: " and everything after the
end of the subject of the email (should be obvious where that is). I figured
that I should use re, so here is what I came up with to match the first
piece of what I want to strip away (up to From);

    head_long = '%s %s' % (num, data[0])
    snip =
re.compile("\d+\s\(\'\d+\s\(BODY\[HEADER\.FIELDS\s\(FROM\sSUBJECT\)\]\s\{\d+
\}\',\s")
    head = snip.sub(head_long, '')

N.B. head_long = the entire line before I strip it (if that was not clear)

The problem is, this seems to replace the entire line with '', instead of
just the part up to "'From: ".  I know you guys must get this all the time,
but can anyone see what I have done wrong?

Thanks if you can help.

tim






More information about the Python-list mailing list