AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Code-Bibliothek Neuen Beitrag zur Code-Library hinzufügen Delphi PosExUltra - Ultimative Stringsuche/Parser
Thema durchsuchen
Ansicht
Themen-Optionen

PosExUltra - Ultimative Stringsuche/Parser

Ein Thema von Novo · begonnen am 21. Feb 2010 · letzter Beitrag vom 19. Mai 2010
 
omata

Registriert seit: 26. Aug 2004
Ort: Nebel auf Amrum
3.154 Beiträge
 
Delphi 7 Enterprise
 
#13

Re: PosExUltra - Ultimative Stringsuche/Parser

  Alt 21. Feb 2010, 20:44
Benötigte Unit: StrUtils, Classes

Novo überarbeitet:
Delphi-Quellcode:
procedure PosExUltra(source, searchwordbefore, searchwordafter,
                     addbefore, addafter: string;
                     continuesearch, reversesearch: boolean;
                     liste: TStrings);

  function Search(var Anfang, Ende, Posi: Integer): string;
  var P: Integer;
  begin
    P:=PosEx(searchwordbefore, source, Posi);
    while (P = 0) or (P > Posi) do begin
      Dec(Posi);
      P:=PosEx(searchwordbefore, source, Posi);
    end;
    Result := Copy(
      source,
      Posi + length(searchwordbefore),
      Anfang - (Posi + length(searchwordbefore))
    );
    if (Result <> '') or (Result <> ' ') then
      liste.Append(addbefore + Result + addafter);
  end;

var
  Anfang, Ende, Posi: Integer;
  Ergebnis: string;
begin
  Ende := 0;
  if reversesearch then begin
    if continuesearch then begin
      Anfang := PosEx(searchwordafter, source, Ende);
      while Anfang > 0 do begin
        Posi := Anfang;
        if Anfang > 0 then begin
          Ergebnis:=Search(Anfang, Ende, Posi);
          Ende :=
              Posi
            + length(searchwordbefore)
            + Length(Ergebnis)
            + Length(searchwordafter);
          Anfang := PosEX(searchwordafter, source, Ende);
        end;
      end;
    end
    else begin
      Anfang := PosEx(searchwordafter, source, Ende);
      Posi := Anfang;
      if Anfang > 0 then
        Ergebnis:=Search(Anfang, Ende, Posi);
    end;
  end
  else if continuesearch then begin
    repeat
      Anfang := PosEx(searchwordbefore, source, Ende);
      if Anfang > 0 then begin
        Ende := PosEx(searchwordafter, source, Anfang);
        Ergebnis := Copy(
          source,
          Anfang + length(searchwordbefore),
          (Ende - Anfang) - length(searchwordbefore)
        );
        if (Ergebnis <> '') or (Ergebnis <> ' ') then
          liste.Append(addbefore + Ergebnis + addafter);
        Anfang := Anfang + length(Ergebnis);
      end;
    until Anfang = 0;
  end
  else begin
    Anfang := PosEx(searchwordbefore, source, Ende);
    if Anfang > 0 then begin
      Ende := PosEx(searchwordafter, source, Anfang);
      Ergebnis := Copy(
        source,
        Anfang + length(searchwordbefore),
        (Ende - Anfang) - length(searchwordbefore)
      );
      if (Ergebnis <> '') or (Ergebnis <> ' ') then
        liste.Append(addbefore + Ergebnis + addafter);
    end;
  end;
end;
alzaimar überarbeitet:
Delphi-Quellcode:
procedure ExtractBetween(const aSource, aPrefix, aSuffix : string;
                         aFindAll : Boolean; aNewPrefix, aNewSuffix : string;
                         aWords : TStrings);
var
  PrefixLength, PosPrefix, PosSuffix, BestPos : Integer;
begin
  PosPrefix := PosEx(aPrefix, aSource, 1);
  if PosPrefix > then begin
    PrefixLength := Length (aPrefix);
    repeat
      PosSuffix := PosEx(aSuffix, aSource, PosPrefix + PrefixLength);
      if PosSuffix > 0 then begin
        while (PosPrefix <> 0) and (PosPrefix < PosSuffix) do begin
          BestPos := PosPrefix;
          PosPrefix := PosEx(aPrefix, aSource, PosPrefix + PrefixLength);
        end;
        aWords.Append(
            aNewPrefix
          + Copy(aSource, BestPos + PrefixLength, PosSuffix-BestPos - PrefixLength)
          + aNewSuffix
        );
      end;
    until not aFindall or (PosPrefix = 0);
  end;
end;
  Mit Zitat antworten Zitat
 


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 18:18 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz