2011-06-02 256 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

如果您需要一個程序來查找「電子郵件地址提取器和驗證器」。