Solved

replacing text in a text file

Posted on 2002-07-02
8
146 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
 
LVL 33

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

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 33

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now