Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

replacing text in a text file

Posted on 2002-07-02
8
Medium Priority
?
155 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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…
The viewer will learn how to count occurrences of each item in an array.

636 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