gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:51:45 25 4
gpt4 key购买 nike

这是我想要解析的电子邮件示例,仅提取其正文。

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.0 秒后捕获了虚线,并卡在非 ascii 字符 © 处。 。谢谢!

PS:我认为最好的方法是用组来切断电子邮件的 header 和尾部。那么剩下的就是 body 了。因为 header 和尾部始终保持不变,但正文在不同的电子邮件上会发生变化。该解决方案不需要特定于电子邮件。

最佳答案

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

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

这将匹配预期的输出:

\*{3}[^\*]*(?:(?=\*{3})|(?=^-*$))
  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

关于python - 正则表达式 解析电子邮件 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14001382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com