gpt4 book ai didi

delphi - 从 Thunderbird 电子邮件中提取电子邮件地址

转载 作者:行者123 更新时间:2023-12-03 15:51:04 25 4
gpt4 key购买 nike

我想提取 Thunderbird 电子邮件文件中找到的所有电子邮件地址。有时,电子邮件会以空格形式出现,有时会以 <> 形式出现,也可能以其他方式出现。我能够找到每个字符串中 @ 出现的位置,但如何获取形成电子邮件的前后字符?

谢谢。

最佳答案

Regex 就是为这种工作而生的。这是一个最小的控制台应用程序,展示了如何使用 RegEx 从一长文本 block 中提取所有电子邮件地址:

program Project25;

{$APPTYPE CONSOLE}

uses
SysUtils, PerlRegex;

var PR: TPerlRegEx;
TestString: string;

begin

// Initialize a test string to include some email addresses. This would normally
// be your eMail text.
TestString := '<one@server.domain.xy>, another@otherserver.xyz';

PR := TPerlRegEx.Create;
try
PR.RegEx := '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'; // <-- this is the actual regex used.
PR.Options := PR.Options + [preCaseLess];
PR.Compile;
PR.Subject := TestString; // <-- tell the TPerlRegEx where to look for matches
if PR.Match then
begin
// At this point the first matched eMail address is already in MatchedText, we should grab it
WriteLn(PR.MatchedText); // Extract first address (one@server.domain.xy)
// Let the regex engine look for more matches in a loop:
while PR.MatchAgain do
WriteLn(PR.MatchedText); // Extract subsequent addresses (another@otherserver.xyz)
end;
finally PR.Free;
end;

Readln;
end.

请参阅此处,了解为较旧的 XE 版本的 Delphi 获取正则表达式的方法: http://www.regular-expressions.info/delphi.html

关于delphi - 从 Thunderbird 电子邮件中提取电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6214945/

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