Einzelnen Beitrag anzeigen

omata

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

Re: PosExUltra - Ultimative Stringsuche/Parser

  Alt 21. Feb 2010, 21: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