?
Solved

replacing text in a text file

Posted on 2002-07-02
8
Medium Priority
?
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 34

Accepted Solution

by:
Slick812 earned 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

801 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