# Function to find text between two tags

I've just finished this function and wanted to know if anyone know another way to do the same:

function findtext(archivo,Delimit1, Delimit2 :String) :String;
var
Buffer      :AnsiString;
ResLength   :Integer;
i           :Integer;
PosDelimit  :Integer;
begin
if Pos(Delimit1, Buffer) > Pos(Delimit2, Buffer) then
PosDelimit := Length(Buffer)-(Pos(Delimit1, Buffer)+Length(Delimit1))
else PosDelimit := Length(Buffer)-(Pos(Delimit2, Buffer)+Length(Delimit2));
Buffer := Copy(Buffer, (Length(Buffer)-PosDelimit), Length(Buffer));
ResLength := Pos(Delimit2, Buffer)-(Pos(Delimit1, Buffer)+Length(Delimit1));
for i := 0 to (Reslength-1) do
Result := Result+Buffer[Pos(Delimit1, Buffer)+(Length(Delimit1)+i)];
end;


The auxiliary function read_file_z is this:

function read_file_z(const FileName: String): AnsiString;
var
F: File;
DefaultFileMode: Byte;
begin
DefaultFileMode := FileMode;
try
FileMode := 0;
AssignFile(F, FileName);
{$I-} Reset(F, 1); {$I+}
if IoResult=0 then
try
SetLength(Result,FileSize(F));
if Length(Result)>0 then begin
{$I-} BlockRead(F,Result[1],LENGTH(Result)); {$I+}
if IoResult<>0 then Result:='';
end;
finally
CloseFile(F);
end;
finally
FileMode := DefaultFileMode;
end;
end;


The function finds text between two tags. An example would [hi]hi world[hi] and the function will respond "hi world".

Example:

findtext('test.exe','[hi]','[hi]');


The way seek to do the function is the "uses" default.

What alternatives do I have to make the function "findtext"?

• Could you give me an example of your file? An does the start and end tag have to be in the same line? – Jens Borrisholt Feb 23 '15 at 13:52
• I can not is a binary application that I use as an agenda login , data is saved in the bottom of the file , the eof – Matt Olsen Feb 23 '15 at 13:54
• So data is stored inside the exe file as a Resource? – Jens Borrisholt Feb 23 '15 at 13:55
• no , is plain text. – Matt Olsen Feb 23 '15 at 13:56
• So you want the text between Delimit1 and Delimit2 ? – Jens Borrisholt Feb 23 '15 at 14:03

You code is extremly hard to read. But I belive this is what you are looking for:

function FindText(FileName: TFilename; const Delimit1, Delimit2: string): string;
var
Buffer: TStringList;
PD1, PD2: Integer;
begin
Buffer := TStringList.Create;
try