![]() |
Re: PosExUltra - Ultimative Stringsuche/Parser
Ich würde die genannte Zeile einfach löschen. Der, der deine Funktion nutzen möchte, soll diese selbst in einen Thread auslagern. Da musst du nichts basteln. ;)
|
Re: PosExUltra - Ultimative Stringsuche/Parser
ok habs mal verbessert.
|
Re: PosExUltra - Ultimative Stringsuche/Parser
Wenn eine Funktion oft sehr lange dauert:
Was du da machen könntest, wäre eine alternativer Parameter, bei welchem man eine Callback-Funktion angeben kann ... dort könnte der Benutzer deiner Funktion dann selber entscheiden, wie er dieses bei sich behandelt. Wobei man bei sowas eigentlich Funktion besser in Threads auslagert. PS: Wenn in deinem Code auf Application zugegriffen würde, dann könnte man diese Funktion nicht in einen Thread auslagern. Allgemein sollte bei derartigen Funktionen sowieso nicht auf Externes zugegriffen werden (dazu zählen Application oder eine Form). |
Re: PosExUltra - Ultimative Stringsuche/Parser
So, ich hab mich auch mal versucht und kurz getestet.
Delphi-Quellcode:
Die ganzen Sonderfälle (leere Suchstrings usw) habe ich nicht abgefangen.
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=0 then exit; PrefixLength := Length (aPrefix); Repeat PosSuffix := PosEx (aSuffix, aSource, PosPrefix + PrefixLength); if PosSuffix=0 then Exit; While (PosPrefix<>0) and (PosPrefix<PosSuffix) Do Begin BestPos := PosPrefix; PosPrefix := PosEx (aPrefix, aSource, PosPrefix + PrefixLength); End; sWords.Add (aNewPrefix + Copy (aSource, BestPos+PrefixLength,PosSuffix-BestPos-PrefixLength) + aNewSuffix); If PosPrefix=0 then exit; Until not aFindall; End; |
Re: PosExUltra - Ultimative Stringsuche/Parser
Benötigte Unit: StrUtils, Classes
Novo überarbeitet:
Delphi-Quellcode:
alzaimar überarbeitet:
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;
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; |
Re: PosExUltra - Ultimative Stringsuche/Parser
Hi omata,
Danke. |
Re: PosExUltra - Ultimative Stringsuche/Parser
Hallo omata,
da hat sich ein kleiner Copy&Paste-Fehler in der 2. Procedure versteckt: In
Delphi-Quellcode:
sollte die letzte Zeile wohl so lauten:
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
Delphi-Quellcode:
Gruß,
if PosPrefix > 0 then begin
Stefan |
Re: PosExUltra - Ultimative Stringsuche/Parser
Hallo,
noch einige Anmerkungen: - Wenn aPrefix gefunden wird, aSuffix aber nicht, kommt es zu einer Endlosschleife für aFindAll = True. Im Code von alzaimar war dieser Fall durch eine Exit-Anweisung abgedeckt. - Der Compiler warnt, dass die Variable BestPos möglicherweise nicht initialisiert wird. - Das Füllen der Stringliste sollte mit einem BeginUpdate..EndUpdate geklammert werden. Ist eine Routine mit sieben bzw. acht Parametern wirklich sinnvoll? Wie testet man solch ein Ungetüm? Gruß Hawkeye |
Re: PosExUltra - Ultimative Stringsuche/Parser
Zitat:
|
Re: PosExUltra - Ultimative Stringsuche/Parser
Zitat:
Zitat:
Hier die Korrektur...
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 > 0 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 (PosSuffix = 0) or (PosPrefix = 0); end; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:52 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