简体   繁体   English

Python / Jython解析电子邮件

[英]Python/Jython parse e-mail

I have parsed en e-mail message using Jython to get the e-mail mesages body value. 我已经使用Jython解析了电子邮件,以获取电子邮件消息正文值。 Now I have the body value and I'd like to extract the following text from it. 现在,我具有主体价值,我想从中提取以下文本。

The body contains text and I would like to extract the following text: 该正文包含文本,我想提取以下文本:

There are rows found in the body: 正文中发现以下行:

 [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

Now I would like to extract all the value after the [description]: Is this possible? 现在,我想提取[说明]之后的所有值:这可能吗? I tried this: 我尝试了这个:

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

A regex possible solution, but consider @StefanNch suggestion: 正则表达式可能的解决方案,但请考虑@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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM