正则表达式解析电子邮件Python

时间:2012-12-22 08:45:11

标签: python regex

所以这是我要解析的电子邮件样本,仅提取其正文。

RECEIVED: 2012-11 20 09:59:24
SUBJECT: Get Boddy
--- Original Sender: Mark Twain. ---

----- Original Message -----
From: Boby Indo
To: Obum Hunter 
At: 11/20  9:59:22

***NEW ISSUE SUPPORTED THROUGH UNIVERSALITY   vs 104-13 on AY 3s JAN   
10+BB {MYXV ABC 4116    SM  MYXV YA 102-15 <DO>} | 2010/11 4.0s             4.0s
6+ BB {MYXV ABC 4132    NS  MYXV YT 102-22 <DO>} | 2010 4.5s                4.5s
ABO 2006-OP1 M1     00442PAG5     19-24      p5 
***SECOND SUPPORTED TRHOUGH INVERSALITY GEVINGS                      
10+BB  {NXTW VXA 4061   SL  MYXV YA 103-22 <DO>} | 11 wala 3.5s             3.5s
10+BB  {NXTW VXA 12-47  SP  MYXV YA 106-20 <DO>} | 22 wala 4.0s             4.0s

------------------------------------------------------------
© Copyright 2012 The Ridgly Group, Inc. All rights reserved. See
http://www.examply.html for important information disclosure.

这就是我的期望:

***NEW ISSUE SUPPORTED THROUGH UNIVERSALITY   vs 104-13 on AY 3s JAN   
10+BB {MYXV ABC 4116    SM  MYXV YA 102-15 <DO>} | 2010/11 4.0s             4.0s
6+ BB {MYXV ABC 4132    NS  MYXV YT 102-22 <DO>} | 2010 4.5s                4.5s
ABO 2006-OP1 M1     00442PAG5     19-24      p5 
***SECOND SUPPORTED TRHOUGH INVERSALITY GEVINGS                      
10+BB  {NXTW VXA 4061   SL  MYXV YA 103-22 <DO>} | 11 wala 3.5s             3.5s
10+BB  {NXTW VXA 12-47  SP  MYXV YA 106-20 <DO>} | 22 wala 4.0s             4.0s

如果***行也可以消除,那就太好了。

这就是我到目前为止所得到的(?P<header>[\S+\s]+At:.*)\n+(?P<body>[\S+\s]([\d\.\d]+[a-z]?$))。这似乎并没有做得很好,因为它在最后的4.0s之后抓住了虚线并且陷入了非ascii角色{ {1}}。谢谢!

PS:我认为最好的方法是用群组切断电子邮件的标题和尾部。剩下的就是身体。因为标题和尾部始终保持不变,但身体在不同的电子邮件中发生变化。解决方案不一定要特定于电子邮件。

2 个答案:

答案 0 :(得分:1)

看看这是否适合您,您想要的行以数字开头,后跟加号:

^[0-9]*\+.*$

这将匹配预期的输出:

\*{3}[^\*]*(?:(?=\*{3})|(?=^-*$))
  
      
  1. ^ 匹配字符串的开头。
  2.   
  3. [0-9] 匹配0-9范围内的任何单个字符。
  4.   
  5. * 匹配前面标记的0个或多个。这是一个贪婪的匹配,在满足下一个标记之前将匹配尽可能多的字符。
  6.   
  7. \ + 匹配+字符。
  8.   
  9. 匹配任何字符。
  10.   
  11. $ 匹配字符串的结尾。
  12.   
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
with open("/path/to/file", "r") as fileInput:
    listLines = [   line.strip()
                    for line in fileInput.readlines()
                    if re.match("^[0-9]*\+.*$", line)
                    ] 


for line in listLines:
    print line

>>> 10+BB {MYXV ABC 4116    SM  MYXV YA 102-15 <DO>} | 2010/11 4.0s             4.0s
>>> 6+ BB {MYXV ABC 4132    NS  MYXV YT 102-22 <DO>} | 2010 4.5s                4.5s
>>> 10+BB  {NXTW VXA 4061   SL  MYXV YA 103-22 <DO>} | 11 wala 3.5s             3.5s
>>> 10+BB  {NXTW VXA 12-47  SP  MYXV YA 106-20 <DO>} | 22 wala 4.0s             4.0s

已更新以满足新要求:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
with open("/path/to/file", "r") as fileInput:
    regex = re.compile(r"\*{3}[^\*]*?(?:(?=^-*$)|(?=\*))", re.MULTILINE)

    listMsg = [ [   line.strip()
                    for line in message.split("\n")
                    if not line.startswith("*") and line.strip()
                    ]
                for message in regex.findall(fileInput.read())
                ]

>>> 10+BB {MYXV ABC 4116    SM  MYXV YA 102-15 <DO>} | 2010/11 4.0s             4.0s
>>> 6+ BB {MYXV ABC 4132    NS  MYXV YT 102-22 <DO>} | 2010 4.5s                4.5s
>>> ABO 2006-OP1 M1     00442PAG5     19-24      p5
>>> 10+BB  {NXTW VXA 4061   SL  MYXV YA 103-22 <DO>} | 11 wala 3.5s             3.5s
>>> 10+BB  {NXTW VXA 12-47  SP  MYXV YA 106-20 <DO>} | 22 wala 4.0s             4.0s

更新以摘录电子邮件的全部内容:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
with open("/path/to/file", "r") as fileInput:
    regex = re.compile(r"(?<=^At:)([^\n\r]*)(.*?)(?=^-*-$)", re.MULTILINE|re.DOTALL)

    print regex.search(fileInput.read()).groups()[1]

>>> ACE 2006-OP1 ZZ 111111111 19-24 Z5 ZZW 2012-0P1 SD 222222222 77-00 150
>>> ***NEW ISSUE SUPPORTED THROUGH UNIVERSALITY   vs 104-13 on AY 3s JAN   
>>> 10+BB {MYXV ABC 4116    SM  MYXV YA 102-15 <DO>} | 2010/11 4.0s             4.0s
>>> 6+ BB {MYXV ABC 4132    NS  MYXV YT 102-22 <DO>} | 2010 4.5s                4.5s
>>> ABO 2006-OP1 M1     00442PAG5     19-24      p5 
>>> ***SECOND SUPPORTED TRHOUGH INVERSALITY GEVINGS                      
>>> 10+BB  {NXTW VXA 4061   SL  MYXV YA 103-22 <DO>} | 11 wala 3.5s             3.5s
>>> 10+BB  {NXTW VXA 12-47  SP  MYXV YA 106-20 <DO>} | 22 wala 4.0s             4.0s

答案 1 :(得分:1)

>>> s="""RECEIVED: 2012-11 20 09:59:24
... SUBJECT: Get Boddy
... --- Original Sender: Mark Twain. ---
... 
... ----- Original Message -----
... From: Boby Indo
... To: Obum Hunter 
... At: 11/20  9:59:22
... 
... ***NEW ISSUE SUPPORTED THROUGH UNIVERSALITY   vs 104-13 on AY 3s JAN   
... 10+BB {MYXV ABC 4116    SM  MYXV YA 102-15 <DO>} | 2010/11 4.0s             4.0s
... 6+ BB {MYXV ABC 4132    NS  MYXV YT 102-22 <DO>} | 2010 4.5s                4.5s
... ABO 2006-OP1 M1     00442PAG5     19-24      p5 
... ***SECOND SUPPORTED TRHOUGH INVERSALITY GEVINGS
... 10+BB  {NXTW VXA 4061   SL  MYXV YA 103-22 <DO>} | 11 wala 3.5s             3.5s
... 10+BB  {NXTW VXA 12-47  SP  MYXV YA 106-20 <DO>} | 22 wala 4.0s             4.0s
... 
... ------------------------------------------------------------
... © Copyright 2012 The Ridgly Group, Inc. All rights reserved. See
... http://www.examply.html for important information disclosure."""
>>> r=r'(?P<header>\*\*\*[^\n]*)\n(?P<body>[\s\S]*?\n)\n'
>>> for match in re.finditer(r, s):
...     print match.group('body')
... 
10+BB {MYXV ABC 4116    SM  MYXV YA 102-15 <DO>} | 2010/11 4.0s             4.0s
6+ BB {MYXV ABC 4132    NS  MYXV YT 102-22 <DO>} | 2010 4.5s                4.5s

10+BB  {NXTW VXA 4061   SL  MYXV YA 103-22 <DO>} | 11 wala 3.5s             3.5s
10+BB  {NXTW VXA 12-47  SP  MYXV YA 106-20 <DO>} | 22 wala 4.0s             4.0s