AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

replace string in text files

Ein Thema von direktor · begonnen am 17. Jan 2020 · letzter Beitrag vom 21. Jan 2020
Antwort Antwort
Seite 1 von 2  1 2      
direktor

Registriert seit: 5. Apr 2014
22 Beiträge
 
#1

replace string in text files

  Alt 17. Jan 2020, 19:22
Hello,

I want to replace certain text in html files. Like I want to replace all text in html file from <header> to </header> and <footer> to </footer> - all text between beginning and ending of headers and footers. How can I do that? It's not stringreplace function. I need to replace all text between this header and footer tags. I can detect with Pos function where <header> begins and ends. So I get 2 integers I and J. I need to replace all text between I and J position of string occurance in text file. Any solutions? Thanks.
  Mit Zitat antworten Zitat
Benutzerbild von Dalai
Dalai

Registriert seit: 9. Apr 2006
1.682 Beiträge
 
Delphi 5 Professional
 
#2

AW: replace string in text files

  Alt 17. Jan 2020, 19:39
Well, the easy way would be something like this: Read the file into a TStringList variable (Delphi-Referenz durchsuchenTStringList.LoadFromFile) and replace everything in Delphi-Referenz durchsuchenTStringList.Text from (and including) <header> to </header> with "<header>your_new_text</header>" with the Delphi-Referenz durchsuchenStringReplace function. However, I doubt that this will be the fastest way in terms of performance (e.g. because the .Text property makes a copy of the whole string).

[EDIT]
It just came to me that StringReplace in Delphi doesn't work with offsets. So, you probably need to determine the offset of <header> and </header> tags with Delphi-Referenz durchsuchenPos function and use Delphi-Referenz durchsuchenCopy function to get the substring between these tags and then use StringReplace on that substring.
[/EDIT]

Regards
Dalai

Geändert von Dalai (17. Jan 2020 um 21:00 Uhr)
  Mit Zitat antworten Zitat
Dennis07

Registriert seit: 19. Sep 2011
Ort: Deutschland
488 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: replace string in text files

  Alt 17. Jan 2020, 22:04
This might work, but is not a really secure and surely not the fastest way.
Have a look at LinaComponents, in WebCtrls.pas, there is a THtmlDocument class. It should give you an idea or perhaps even do exactly that. The problem with HTML are for example the comments, or text inside strings, that need to be ignored.
The source for the parsing goes as follows (original can be found in LinaComponent's WebCtrls unit):
Delphi-Quellcode:
procedure THtmlDocumentTag.LinesChange(Sender: TObject);
var
  Index: Integer;
  Current: PChar;
  CurrentTag: String;
  TagName: String;
  TagText: String;
  TagLines: TStrings;
  InTag, InTagArea: Boolean;
  TagKind: (tagOpen,tagClose,tagSingle,tagComment);
begin
  for Index := 0 to TagCount - 1 do
  begin
    Tags[Index].Free;
  end;
  SetLength(FTags,0);
  FText := '';
  TagName := '';
  TagText := '';
  TagLines := TStringList.Create;
  InTag := False;
  InTagArea := False;
  Current := PChar(Lines.Text);
  try
    try
      while Current^ <> #0 do
      begin
        if (Current^ = '<') and ((Current + 1)^ in Letters + ['!','/']) then
        begin
          InTag := True;
          InTagArea := True;
          if (Current + 1)^ in Letters then
          begin
            TagKind := tagOpen;
            if Length(TagName) <> 0 then
            begin
              TagText := TagText + Current^;
            end;
            //-->
            Inc(Current);
            Continue;
          end;
          if ((Current + 1)^ = '!') and ((Current + 2)^ = '-') and ((Current + 3)^ = '-') then
          begin
            TagKind := tagComment;
            if Length(TagName) <> 0 then
            begin
              TagText := TagText + Current^ + (Current + 1)^ + (Current + 2)^ + (Current + 3)^;
            end;
            //-->
            Inc(Current,4);
            Continue;
          end;
          if ((Current + 1)^ = '/') and ((Current + 2)^ in Letters) then
          begin
            TagKind := tagClose;
            //-->
            Inc(Current,2);
            Continue;
          end;
        end;
        if InTagArea then
        begin
          if InTag then
          begin
            case TagKind of
              tagSingle,
              tagOpen: if (Current^ = '>') or ((Current^ = '/') and (Length(Trim(CurrentTag)) <> 0)) then
                       begin
                         if Length(CurrentTag) <> 0 then
                         begin
                           InTag := False;
                           if Length(TagName) = 0 then
                           begin
                             TagName := CurrentTag;
                           end else
                           begin
                             TagText := TagText + Current^;
                           end;
                           CurrentTag := '';
                           //Parse spaces and parameters .... (to be added)
                           if Current^ = '/then
                           begin
                             TagKind := tagSingle;
                             if Length(TagText) <> 0 then
                             begin
                               TagText := TagText + Current^;
                             end;
                           end;
                           //-->
                           Inc(Current);
                           Continue;
                         end;
                       end else
                       begin
                         CurrentTag := CurrentTag + Current^;
                         if Length(TagName) <> 0 then
                         begin
                           TagText := TagText + Current^;
                         end;
                         //-->
                         Inc(Current);
                         Continue;
                       end;
              tagClose: if Current^ = '>then
                        begin
                          if (Length(CurrentTag) <> 0) and (Length(TagName) <> 0) then
                          begin
                            InTag := False;
                            if TrimRight(CurrentTag) = TagName then
                            begin
                              InTagArea := False;
                              SetLength(FTags,Length(FTags) + 1);
                              TagLines.Text := TagText;
                              FTags[TagCount - 1] := THtmlDocumentTag.Create(TagName,Document,Self,TagLines);
                              if Assigned(Document.OnTagAdd) then
                              begin
                                Document.OnTagAdd(Self,FTags[TagCount - 1]);
                              end;
                              TagLines.Clear;
                              TagName := '';
                              TagText := '';
                            end else
                            begin
                              TagText := TagText + '</' + CurrentTag + '>';
                            end;
                            CurrentTag := '';
                            //-->
                            Inc(Current);
                            Continue;
                          end;
                        end else
                        begin
                          CurrentTag := CurrentTag + Current^;
                          //-->
                          Inc(Current);
                          Continue;
                        end;
              tagComment: if (Current^ = '-') and ((Current + 1)^ = '-') and ((Current + 2)^ = '>') then
                          begin
                            InTag := False;
                            TagText := TagText + Current^ + (Current + 1)^ + (Current + 2)^;
                            //-->
                            Inc(Current,3);
                            Continue;
                          end else
                          begin
                            TagText := TagText + Current^;
                            //-->
                            Inc(Current);
                            Continue;
                          end;
            end;
          end else
          begin
            TagText := TagText + Current^;
            //-->
            Inc(Current);
            Continue;
          end;
        end else
        begin
          FText := FText + Current^;
          //-->
          Inc(Current);
          Continue;
        end;
        raise EHtmlParse.Create('HTML parse error on line ' + IntToStr(CharLine(Current,Lines.Text)) + ' at position ' + IntToStr(CharPosition(Current,Lines.Text)));
      end;
      if InTagArea then
      begin
        SetLength(FTags,Length(FTags) + 1);
        TagLines.Text := TagText;
        FTags[TagCount - 1] := THtmlDocumentTag.Create(TagName,Document,Self,TagLines);
        if Assigned(Document.OnTagAdd) then
        begin
          Document.OnTagAdd(Self,FTags[TagCount - 1]);
        end;
      end;
    finally
      TagLines.Free;
    end;
  except
    raise;
  end;
end;
Dennis

Geändert von Dennis07 (17. Jan 2020 um 22:06 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.475 Beiträge
 
Delphi 12 Athens
 
#4

AW: replace string in text files

  Alt 17. Jan 2020, 22:12
Ignoring the problems mentioned by Dennis07 regarding comments:
Delphi-Quellcode:
function ReplaceBetweenTags(const Source, TagStart, TagEnd, ReplaceWith: string): string;
var
  posEnd: Integer;
  posStart: Integer;
begin
  posStart := Source.IndexOf(TagStart) + TagStart.Length;
  posEnd := Source.IndexOf(TagEnd, posStart);
  Result := Source.Substring(0, posStart) + ReplaceWith + Source.Substring(posEnd);
end;
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
direktor

Registriert seit: 5. Apr 2014
22 Beiträge
 
#5

AW: replace string in text files

  Alt 18. Jan 2020, 06:55
Dalai text is different every time so I can't use stringreplace function. I was thinking of creating 2 TStringLists and put together the whole file 1. copy up from header + 2. header replacement + 3. copy rest of file... Dennis thanks, long solution but I'll try. Uwe - Sie sind genial! Thank you all.
  Mit Zitat antworten Zitat
Benutzerbild von Dalai
Dalai

Registriert seit: 9. Apr 2006
1.682 Beiträge
 
Delphi 5 Professional
 
#6

AW: replace string in text files

  Alt 18. Jan 2020, 14:49
Dalai text is different every time so I can't use stringreplace function.
See my EDIT. StringReplace can be used after determining the tags' offsets and getting the text via Copy function (using the previously determined offsets).

Regards
Dalai
  Mit Zitat antworten Zitat
direktor

Registriert seit: 5. Apr 2014
22 Beiträge
 
#7

AW: replace string in text files

  Alt 18. Jan 2020, 20:57
I have another problem, how to pass the Space in string?

For example I want to find string 'this is just a test' and replace it with 'this is not a test'. Problem is Delphi will find only first word 'this' - everything after space is ignored. How do I make it find full string not that it stops at space? Or I'm using Delphi version which is too low? Should I use Unicode string compare?

'this is something else' - delphi will find stop here because 'this' is contained here
'this is just a test' - but I want to change this line of text

How to do?
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#8

AW: replace string in text files

  Alt 18. Jan 2020, 21:16
Please open a new topic for a new problem. Thank you.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
TurboMagic

Registriert seit: 28. Feb 2016
Ort: Nordost Baden-Württemberg
2.960 Beiträge
 
Delphi 12 Athens
 
#9

AW: replace string in text files

  Alt 20. Jan 2020, 22:00
Another question: why so people write in English in German
Delphi-Praxis Forum eben there is an english one available nie?
  Mit Zitat antworten Zitat
Benutzerbild von p80286
p80286

Registriert seit: 28. Apr 2008
Ort: Stolberg (Rhl)
6.659 Beiträge
 
FreePascal / Lazarus
 
#10

AW: replace string in text files

  Alt 20. Jan 2020, 23:39
Es könnte sein, daß sie einfach höflich sind und nicht platt auf die englische Version verweisen.

Gruß
K-H
Programme gehorchen nicht Deinen Absichten sondern Deinen Anweisungen
R.E.D retired error detector
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:01 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz