2011-06-02 259 views
3

我想提取在Thunderbird电子邮件文件中找到的所有电子邮件地址。有时电子邮件会在空间中放置,有时可能会以其他方式存在。我能够找到每个字符串出现@的位置,但是如何在形成电子邮件之前和之后抓住字符?从Thunderbird中提取电子邮件地址电子邮件

谢谢。

回答

5

正则表达式出生于这种工作。这里有一个最小的控制台应用程序,演示了如何使用正则表达式从文本的一个长块中提取的所有电子邮件地址:

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 := '<[email protected]>, [email protected]'; 

    PR := TPerlRegEx.Create; 
    try 
    PR.RegEx := '\b[A-Z0-9._%+-][email protected][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 ([email protected]) 
     // Let the regex engine look for more matches in a loop: 
     while PR.MatchAgain do 
     WriteLn(PR.MatchedText); // Extract subsequent addresses ([email protected]) 
    end; 
    finally PR.Free; 
    end; 

    Readln; 
end. 

在这里看到的方式来获得正则表达式德尔福的较旧的,然后-XE版本: http://www.regular-expressions.info/delphi.html

+0

谢谢,这非常有用。 – 2011-06-02 14:01:52

+0

@downvoter,为什么downvote?您是否注意到显示解决方案*的* complete *控制台应用程序*?如果您确实发现错误,您是否介意分享? – 2011-06-02 14:19:37

+0

@Cosmin - 也许downvoter不喜欢控制台应用程序:)无论如何,我是+1。 – 2011-06-02 14:36:07

0

如果您需要一个程序来查找“电子邮件地址提取器和验证器”。