Extract emails from a Memo

Asked

Viewed 411 times

3

I have a component memo, within it there is a large text, and in that text it contains several scattered email addresses.

How can I extract only the emails from this memo1 and play at memo2?

  • Put part of the text that is in memo1, they are separated by some standard?

  • @rray, not without default, is a large text and within it there are scattered several email addresses as follows: [email protected], or [email protected]. I need to create a way to track this memo and capture only the accounts and put in the other memo, I’m trying to use regular expressions because it’s faster right, more I’m not getting!

  • At least they’re separated by space?

  • @rray, more or less like this friend: [email protected] 32l32k323l23232322222222222222222222222222222222222222222 [email protected]

2 answers

0

From the example you left in the comment it seems that the emails are separated by spaces between the text, it was enough to search line by line for the blank spaces for each word you find we passed for a variable and we checked if the word can correspond to an email, if yes then we keep, and proceed to the next word.

To test, you need to create a new project, add 1 button, 2 memos, and copy the code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  //escreve texto no memo
  Memo1.Text := '[email protected] 32l32k323l23232@2222222  2.2.22 [email protected] 3434534 dsf s32234324 [email protected]  adas asdfaaaaaaaaaaaa [email protected]';
end;

procedure TForm1.Button1Click(Sender: TObject);
var i, APos: Integer;
    ALine, AWord, ALetter, AEmail: String;
    FoundAT, FoundDOT: boolean;
begin
  //vai correr as linhas todas do memo
  for i := 0 to Memo1.Lines.Count - 1 do
    Begin
      ALine := Memo1.Lines[i];

      //enquanto a linha tiver texto verifica
      While ALine <> '' do
        Begin
          FoundAT := False;
          FoundDOT := False;
          AEmail := '';

          //procuro espaços o próximo espaço " " 
          APos := Pos(' ', ALine);
          if (APos > 0) then
            begin
              //pego a palavra e apago-a da variável ALine
              AWord := Trim(AnsiMidStr(ALine, 1, APos - 1));
              Delete(ALine, 1, APos);
            end
          else
            Begin
              //quando não encontramos mais " " então pegamos o resto da linha  
              AWord := ALine;
              ALine := '';
            End;

          AEmail := AWord;

          //vamos analisar letra a letra 
          while AWord <> '' do
            Begin
              ALetter := Trim(AnsiMidStr(AWord, 1, 2 - 1));
              Delete(AWord, 1, 1);

              //procuramos pelo arroba e pelo ponto 
              if (ALetter = '@') then FoundAT := True
              else if (ALetter = '.') then FoundDOT := True;

              //se encontrou arroba e ponto então erscreve no memo2 o email
              if (FoundAT = True) and (FoundDOT = True) then
                Begin
                  AWord := '';
                  Memo2.Lines.Add('Email = ' + AEmail);
                End;
            End;
        End;
    End;
end;

I hope I’ve helped. Any questions let me know.

0

The following routine would work in Python, you could try converting to use the Delphi class of regular expressions. Remembering that there is no regular expression that takes 100% of valid emails, but good expressions take 99.9...%.

import re
import sys

email_regex = "[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}"
regex = re.compile(email_regex)

data = open(sys.argv[1]).read()

for email in regex.finditer(data):
    print data[email.start(0):email.end(0)]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.