Solved

Text Search

Posted on 2003-10-21
7
154 Views
Last Modified: 2010-04-05
Hi Experts!  Okay, I need to search a text file and retrieve info from it.  My info will be located between brackets: <info>.  Is there a procedure that can do this for me?  Any examples?
0
Comment
Question by:jennifere
7 Comments
 

Expert Comment

by:adidi
ID: 9592355
hi there - here is my ideea , i don't know if it is the best but is an ideea

load the whole text file in a TRichEdit and then use the funtion "pos" , this function will give the position of the first mached string in the richedit
0
 

Author Comment

by:jennifere
ID: 9593303
That would work, but I don't know the string I'm looking for.  I want to search the text and find the address that will be enclosed in the <>.  I need find the first <, then read everything in between that and the last >.  
0
 
LVL 3

Accepted Solution

by:
philly_tee earned 20 total points
ID: 9596140
Hi,

You can load the file into a memo or richedit, (visible set to false if you dont want to see it), then use this to extract the text between the < >

Memo1.Text := Copy(Memo1.Text, Pos('<', Memo1.Text) + 1, Pos('>', Memo1.Text) - Pos('<', Memo1.Text) - 1);

all i am doing is copying from the '<' a number of characters defined by how far apart the '<' and '>' are. (If that makes sense?)

HTH

Philip
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Expert Comment

by:waldude
ID: 9597225
I don't have an example, but a sugestion would be to use regular expressions. A Regular expressions search would let you do what your wanting. Check out these sites:

http://www.latiumsoftware.com/en/articles/00009.php

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56reconIntroductionToRegularExpressions.asp

Or just Google for Regular Expressions, you'll find lots.
Good luck.

Brandon
0
 

Expert Comment

by:bullsrl
ID: 9598909
I don't claim to be an expert but this is what I came up with, it loads from a text file and output's your <Info> to another text file hope it works

James

procedure FindInfo();
   var
   InFile, OutFile: TextFile;
   Line, InfoString: string;
   Lpointer, LEnd: PChar;
   Info, EndOfInfo: Boolean;
   LeftBracket, RightBracket: Char;
begin
   Info := False;
   EndOfInfo := False;
   LeftBracket := '<';
   RightBracket := '>';

   AssignFile(InFile, 'C:\test.txt');
   AssignFile(OutFile, 'C:\finsihed.txt');
   try
   begin
      Reset(InFile);
      Rewrite(OutFile);
      while not Eof(InFile) do
      begin
         //Read the line
         Readln(InFile, Line);
         LPointer := Strscan(PChar(Line), LeftBracket);
         if LPointer <> nil then
         begin
            Info := True;
         end;
         if Info then
         begin
            LPointer := LPointer + 1;
            LEnd := StrScan(LPointer, RightBracket);
            if LEnd <> nil then
            begin
               LEnd := StrLCopy(LEnd, LPointer, Strlen(PChar(LPointer)) - StrLen(LEnd));
               Info := False;
            end
            else
            begin
               InfoString := LPointer;
               while not EndOfInfo do
               begin
                  Readln(InFile, Line);
                  lEnd := StrScan(PChar(Line), RightBracket);
                  if LEnd <> nil then
                  begin
                     LEnd := StrLCopy(LEnd, PChar(Line), Strlen(PChar(Line)) - StrLen(LEnd));
                     EndOfInfo := True;
                     InfoString := InfoString + ' ' + LEnd;
                  end
                  else
                     InfoString :=  InfoString + ' ' + Line;
               end;
            end;
            Writeln(OutFile, InfoString);
         end;
      end;
   end;
   CloseFile(InFile);
   CloseFile(OutFile);
   except
   end;
end;
0
 

Author Comment

by:jennifere
ID: 9600008
Thanks for all your comments!  I going to give the points to philly_tee, because his solution worked within the first five minutes.
0
 
LVL 3

Expert Comment

by:philly_tee
ID: 9602651
Thanks for the points,

Glad I could help :)

Philip
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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

11 Experts available now in Live!

Get 1:1 Help Now