Solved

replacing text in a text file

Posted on 2002-07-02
8
149 Views
Last Modified: 2013-11-18
hi everyone,

i need to replace a string inside a text file, the string is random, meaning, it could be anything, it's not a fixed string. what i know is that it is enclosed like in HTML and XML inside <> and that the open tag is <name> and the close is </name>

so basically i need find the opening tag and delete the string beginning (and including) the open tag all the way up to the close tag. example:

line 1 some text
line 2<mytag>some random string</mytag>
line 3 some other text

i need to delete the whole thing at the end i'll have

line 1 some text
line 2
line 3 some other text

any ideas?

thx!
0
Comment
Question by:urif
  • 3
  • 3
  • 2
8 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7124809
As long as the two tags are in the same line it is easy.
Source hacked here:

var
  I: Integer;
  List: TStringList;
  S1, S2: Integer;
begin
  List := TStringList.Create;
  List.LoadFromFile('whatevername.txt');
  for I := 0 to List.Count-1 do
  begin
    S1 := Pos('<mytag>', List[I]);
    if S1 > 0 then
    begin
      S2 := Pos('</mytag>', List[I]);
      if S2 > S1 then
        List[I] := Copy(List[I], 1, S1-1) + Copy(List[I], S2 + 8, Length(List[I]));
      // 8 = Length('</mytag>')
    end;
  end;
  List.SaveToFile('whatevername.txt');
  List.Free;
end;
 
0
 

Author Comment

by:urif
ID: 7124839
where is the actual deletion occures? in the copy?

how?
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7125314
Please, learn Delphi first.
Of course! It copies the string part (a line) before the first tag and the string part after the second tag and concatenates them. That new string is assigned to the original string.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 34

Accepted Solution

by:
Slick812 earned 50 total points
ID: 7125537
you might consider using the Delete procedure

procedure Delete(var S: string; Index, Count:Integer);

Description

Delete removes a substring of Count characters from string S starting with S[Index]. S is a string-type variable. Index and Count are integer-type expressions.

If Index is larger than the length of S, no characters are deleted. If Count specifies more characters than remain starting at the S[Index], Delete removes the rest of the string.


 Pos1 := Pos('<mytag>', SList[i]);
   if Pos1 > 0 then
   begin
     Pos2 := Pos('</mytag>', SList[i]);
     Delete(List[I], S1, S2 - S1+ Length('</mytag>'));

also you might load the entire text file into a Single string and search that string
0
 
LVL 34

Expert Comment

by:Slick812
ID: 7125541
sorry, I'm asleep at the wheel

Pos1 := Pos('<mytag>', SList[i]);
Delete(List[I], Pos1, Length('</mytag>'));
0
 

Author Comment

by:urif
ID: 7125792
thanks, with the dlete procedure it works perfect.

robert_marquardt, i wouldn't be asking here if i'd already know how to do it, so please when you answer a question do not asnwer: first learn delphi
0
 
LVL 34

Expert Comment

by:Slick812
ID: 7128807
this is a little late but here is some code for a tag removever that loads the whole file into one string AND searches for the tags case insensitive (small or large letters)


function TForm1.RemoveTags(FileName, Tag: String): Integer;
var
TxtStream: TFileStream;
TxtStr1, PosStr, sTag, eTag: String;
Pos1, Pos2: Integer;
begin
Result := 0;
if not FileExists(FileName) then
  begin
  Result := -1;
  Exit;
  end;

TxtStream := TFileStream.Create(FileName, fmOpenReadWrite     or fmShareDenyWrite);
try
  if TxtStream.Size < 1 then Exit;
  Tag := LowerCase(Tag);
  sTag := '<'+Tag+'>';
  eTag := '</'+Tag+'>';
  SetLength(TxtStr1, TxtStream.Size);
  TxtStream.Read(Pointer(TxtStr1)^, TxtStream.Size);
  PosStr := LowerCase(TxtStr1);
  Pos1 := Pos(sTag,PosStr);
  if Pos1 < 1 then Exit;
  while Pos1 > 0 do
    begin
    Inc(Result);
    Pos2 := Pos(eTag,PosStr);
    if Pos2 > Pos1 then
      begin
      Delete(TxtStr1, Pos1, Pos2-Pos1+Length('</joke>'));
      PosStr := LowerCase(TxtStr1);
      end
      else Exit;
    Pos1 := Pos(sTag,PosStr);
    end;
  TxtStream.Position := 0;
  TxtStream.Write(Pointer(TxtStr1)^, Length(TxtStr1));
  TxtStream.Size := Length(TxtStr1);
finally
TxtStream.Free;
end;

end;

procedure TForm1.Button_ReadTxtClick(Sender: TObject);
var
NumTags: Integer;
MesStr: String;
begin
NumTags := RemoveTags('C:\Stuff\test.txt', 'TheTag');
if NumTags < 0 then
MesStr := 'File does not exist'
else MesStr := IntToStr(NumTags)+' Tags were found in the file';
ShowMessage(MesStr);
end;

- - - - - - - - - - - - - -
I come here also to learn delphi
0
 

Author Comment

by:urif
ID: 7129067
Slick812

thanks for the example, the previous example works really well, yours seems to be the same only better coded. thanks again for the help, i really apreciate it.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

839 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