[英]Python : Extract mails from the string of filenames
我想從文件名中獲取郵件。 以下是一組文件名示例:
string1 = "benoit.m.fontaine@outlook.fr_2022-05-11T11_59_58+00_00.pdf"
string2 = "jeane_benrand@toto.pt_test.pdf"
string3 = "rosy.gray@amazon.co.uk-fdsdfsd-saf.pdf"
我想按部分拆分文件名。 第一個將包含電子郵件,第二個是其余的。 所以它應該給 string2 :
['jeane_benrand@toto.pt', '_test.pdf']
我嘗試了這個正則表達式函數,但是它不適用於第二個和第三個字符串。
email = re.search(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", string)
謝謝您的幫助
鑒於您提供的示例,您可以執行以下操作:
import re
strings = ["benoit.m.fontaine@outlook.fr_2022-05-11T11_59_58+00_00.pdf",
"jeane_benrand@toto.pt_test.pdf",
"rosy.gray@amazon.co.uk-fdsdfsd-saf.pdf"]
pattern = r'([^@]+@[\.A-Za-z]+)(.*)'
[re.findall(pattern, string)[0] for string in strings]
輸出:
[('benoit.m.fontaine@outlook.fr', '_2022-05-11T11_59_58+00_00.pdf'),
('jeane_benrand@toto.pt', '_test.pdf'),
('rosy.gray@amazon.co.uk', '-fdsdfsd-saf.pdf')]
郵件模式解釋([^@]+@[\.A-Za-z]+)
:
[^@]+
: 除@
以外的任意字符組合@
:在[\.A-Za-z]+
:字母和點的任意組合休息模式說明(.*)
(.*)
: 任意字符組合
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.