Python/Jython 解析电子邮件

Python/Jython parse e-mail

我已使用 Jython 解析电子邮件以获取电子邮件正文值。 现在我有了正文值,我想从中提取以下文本。

正文中包含文字,我想提取以下文字:

正文中找到行:

 [type]: mail
 [category]: Values
 [service]: testing
 [description]: Testing out automapping of email
 Line break Testing out automapping of email
 Line break Testing out automapping of email

现在我想提取[description]后面的所有值: 这可能吗? 我试过这个:

desc = '[description]:'
res = findall("{}.*".format(desc), body)[0]

正则表达式可能的解决方案,但考虑@StefanNch 的建议:

\[description\]:((?:.+\n?)*)

import re
p = re.compile(ur'\[description\]:((?:.+\n?)*)')
test_str = u" [type]: mail\n [category]: Values\n [service]: testing\n [description]: Testing out automapping of email\n Line break Testing out automapping of email\n Line break Testing out automapping of email"
subst = u""

result = re.sub(p, subst, test_str)

re.search(p, test_str)

DEMO