Regex解析电子邮件Python

2024-04-25 08:24:03 发布

您现在位置:Python中文网/ 问答频道 /正文

这里是一个电子邮件的示例,我想解析它,只提取它的正文。在

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.

我的期望是:

^{pr2}$

如果***行也能被消除,那就太好了。在

这是我到目前为止得到的结果(?P<header>[\S+\s]+At:.*)\n+(?P<body>[\S+\s]([\d\.\d]+[a-z]?$))。这似乎做得不好,因为它在最后4.0秒后抓取了短划线,并在非ascii字符©处卡住了。谢谢!在

PS:我认为最好的方法是切断邮件的邮件头和邮件尾。剩下的就是尸体了。因为邮件的头和尾总是保持不变,但是邮件的正文会在不同的电子邮件中发生变化。解决方案不必针对电子邮件。在


Tags: 示例电子邮件邮件doatsupportedabcbb
2条回答
>>> 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

看看这对您是否有效,您需要的行以数字开头,后跟加号:

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

这将与预期输出相匹配:

^{pr2}$
  1. ^ Matches the beginning of the string.
  2. [0-9] Matches any single character in the range 0-9.
  3. * Matches 0 or more of the preceeding token. This is a greedy match, and will match as many characters as possible before satisfying the next token.
  4. \+ Matches a + character.
  5. . Matches any character.
  6. $ Matches the end of the string.
#!/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

相关问题 更多 >