我想從檔案名中獲取郵件。以下是一組檔案名示例:
string1 = "[email protected]_2022-05-11T11_59_58 00_00.pdf"
string2 = "[email protected]_test.pdf"
string3 = "[email protected]"
我想按部分拆分檔案名。第一個將包含電子郵件,第二個是其余的。所以它應該給 string2 :
['[email protected]', '_test.pdf']
我嘗試了這個正則運算式函式,但是它不適用于第二個和第三個字串。
email = re.search(r"[a-z0-9\.\- _] @[a-z0-9\.\- _] \.[a-z] ", string)
感謝您的幫助
uj5u.com熱心網友回復:
鑒于您提供的示例,您可以執行以下操作:
import re
strings = ["[email protected]_2022-05-11T11_59_58 00_00.pdf",
"[email protected]_test.pdf",
"[email protected]"]
pattern = r'([^@] @[\.A-Za-z] )(.*)'
[re.findall(pattern, string)[0] for string in strings]
輸出:
[('[email protected]', '_2022-05-11T11_59_58 00_00.pdf'),
('[email protected]', '_test.pdf'),
('[email protected]', '-fdsdfsd-saf.pdf')]
郵件模式說明([^@] @[\.A-Za-z] ):
[^@]: 任何字符組合,除了@@: 在[\.A-Za-z]: 字母和點的任意組合
休息模式解釋(.*)
(.*): 任意字符組合
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/472948.html
上一篇:保存Outlook電子郵件
