[英]Extract Email Address from Thunderbird Emails
我想提取在 Thunderbird email 文件中找到的所有 email 地址。 有时电子邮件以空格的形式,有时以 <> 和可能的其他方式。 我能够找到每个字符串上出现 @ 的位置,但是如何获取形成 email 之前和之后的字符?
谢谢。
正则表达式就是为这种工作而生的。 这是一个最小的控制台应用程序,它展示了如何使用 RegEx 从一个长文本块中提取所有 email 地址:
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.
请参阅此处了解为您的旧版 Delphi 获取正则表达式的方法: http://www.regular-expressions.info/delphi.html
如果您需要执行此操作的程序,请查找“Power Email 地址提取器和验证器”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.