email regexp with extras

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!
LVL 1
hibbidijiAsked:
Who is Participating?
 
Amir AzhdariCommented:
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
 
Amir AzhdariCommented:
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
 
hibbidijiAuthor Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
hibbidijiAuthor Commented:
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
 
Eddie ShipmanAll-around developerCommented:
0
 
hibbidijiAuthor Commented:
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
 
Eddie ShipmanAll-around developerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.