Solved

email regexp with extras

Posted on 2004-10-28
174 Views
Last Modified: 2011-10-03
Hi all.   I want to do this:

Give a file open box that will open a standard text file and read it as a stream or something (I'm flexible.)  This will be a delimited file that will have the emails from my CoREA mailing list in it.   Depending which version of my server I'm using it will either be comma delimited or pipe | delimited.   I would love to see a function that would extract the emails here and put them in a tstringlist.  Any ideas?

Thanks!
0
Question by:hibbidiji
    7 Comments
     
    LVL 6

    Expert Comment

    by:Amir Azhdari
    do you mean something like to this code ? :

    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        OpenDialog1: TOpenDialog;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    var t:textfile;
        s:String;
        list:tstrings;
    begin
    if opendialog1.Execute then
    begin
    list:=tstringlist.Create;
    assignfile(t,opendialog1.FileName);
    reset(t);
    while not eof(t) do
      begin
       readln(t,s);
       list.Add(s);
      end;

    // show the list in the memo
    memo1.lines:=list;
    list.Free;
    end;
    end;

    end.




    // DFM FILE
    object Form1: TForm1
      Left = 192
      Top = 107
      Width = 696
      Height = 480
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object Button1: TButton
        Left = 263
        Top = 137
        Width = 75
        Height = 25
        Caption = 'Button1'
        TabOrder = 0
        OnClick = Button1Click
      end
      object Memo1: TMemo
        Left = 84
        Top = 189
        Width = 457
        Height = 155
        Lines.Strings = (
          'Memo1')
        TabOrder = 1
      end
      object OpenDialog1: TOpenDialog
        Left = 179
        Top = 132
      end
    end
    0
     
    LVL 1

    Author Comment

    by:hibbidiji
    I can do that...  opening a txt file and assigning it to a stream or a tstringlist or whatever.  its running through and picking out emails that are comma delimited on each line that i dont know.   I suck at regexp and have no experiance at ALL with it on delphi
    0
     
    LVL 6

    Accepted Solution

    by:
    then , what about this code :

    function getlist(list1:tstrings;flag:boolean):tstrings; // flag=true it means it's calling from server1
                                                            // flag=false it means it's calling from server2
    var i,j:integer;
        templist:tstrings;
    begin
     result:=tstringlist.Create;
     templist:=tstringlist.Create;
     templist.Clear;
      result.Clear;

    if flag then
      begin  // check for delimited char on SERVER1
      templist.Delimiter:=','; // <---
      for i:=0 to list1.Count-1 do
       begin
       templist.DelimitedText:=list1.Strings[i];
          for j:=0 to templist.count-1 do
            if result.IndexOf(templist.Strings[j])<0 then // for redunduncy check
                      result.Add(templist.Strings[j]);
       end
      end
     else
      begin
      // check for delimited char on SERVER2
      templist.Delimiter:='|'; // <---
      for i:=0 to list1.Count-1 do
       begin
       templist.DelimitedText:=list1.Strings[i];
          for j:=0 to templist.count-1 do
            if result.IndexOf(templist.Strings[j])<0 then // for redunduncy check
                      result.Add(templist.Strings[j]);
       end;
       templist.Free;
      end;

    end;

    procedure TForm1.Button1Click(Sender: TObject);
    var

      FromF: file;
      NumRead: Integer;
      Buf: array[1..2048] of Char;
      list1,mylist:tstrings;
      i:integer;

    begin
      if OpenDialog1.Execute then
      begin
       list1:=tstringlist.Create;
       mylist:=tstringlist.Create;

       list1.Clear;
        AssignFile(FromF, OpenDialog1.FileName);
        Reset(FromF, 1);
          repeat
            BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
            buf[numread+1]:=chr(0);
            list1.Text:=list1.Text+buf;
          until (NumRead = 0) ;
       CloseFile(FromF);

    // get the list for server1
    memo1.Lines:=getlist(list1,true { or false for server2 });

    //    or
    //    mylist:=getlist(list1,true { or false for server2 })
        end;
    end;

    0
     
    LVL 1

    Author Comment

    by:hibbidiji
    That does good with the delimiter, but of maybe 10 things in a line, I have to pick out the email.    so I have to go through the templist and do a regular expression check to find the email in the line then pop it into a list.  

    0
     
    LVL 26

    Expert Comment

    by:EddieShipman
    0
     
    LVL 1

    Author Comment

    by:hibbidiji
    My problem is that I dont know how to do regexp in delphi. I've never used it there before.  all my regexp experiance is in php
    0
     
    LVL 26

    Assisted Solution

    by:EddieShipman
    Download one of the regex controls from Torry's:

    Regular Expressions for Delphi by Andrey Sorokin.  
    http://www.torry.net/vcl/vcltools/text/regexpr.zip

    Pattern String Engine by SoftComplete Development
    http://www.torry.net/vcl/vcltools/text/pse.zip

    TmkreExpr by M.C. van der Kooij
    http://www.torry.net/vcl/vcltools/text/kregex.zip

    UCFileGrep by UCSoft
    http://www.torry.net/vcl/vcltools/text/ucfg.zip



    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    This video discusses moving either the default database or any database to a new volume.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now