2015-06-02 61 views
-1

我已經使用Jython解析了電子郵件消息以獲取電子郵件消息正文值。 現在我有身體的價值,我想從中提取以下文本。Python/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 

現在我想提取[描述後,所有的值]: 這可能嗎? 我嘗試這樣做:

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

你說,體內含有HTML **和* *文本。 HTML在哪裏? – Squall

+0

對不起更新的問題: – user2023042

+0

確定使用這個:res = findall(「%s。*」%'[description]:',body)我只得到一行..我如何包含文本的所有行? – user2023042

回答

2

一個正則表達式可能的解決方案,但考慮@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

+0

嗨,感謝您的建議,但由於產品限制,我無法使用BeautifulSoup。好的,打印時會輸出一個真值。如何打印[說明]後面的文本:? – user2023042

+0

print re.findall(p,text)這將打印想要的結果,但它也會打印[描述]:當它只應打印之後的值。任何線索? – user2023042

+0

我使用捕獲組更新了答案,所以它只捕獲'[description]:' –