Solved

replacing text in a text file

Posted on 2002-07-02
8
151 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

749 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