Registriert seit: 10. Apr 2003
Ort: Heidelberg
360 Beiträge
Delphi 7 Professional
|
Re: Match funktion?
1. Mai 2005, 09:25
Hi,
ich hab dafür folgenden Code geschrieben, den ich allerdings erst 2 Wochen testen konnte, also ohne Gewähr Für Verbesserungen bin ich jederzeit offen:
Delphi-Quellcode:
// Erlaubte Wildcards:
// * = alles oder nichts
// ? = einzelnes zeichen MUSS vorkommen
// # = Zahl MUSS vorkommen
function WildCardMatch(const check, mask:string):boolean;
var _next_wc:integer;
a,b:string;
function find_wildcard(const _check:string):integer;
begin
if (Pos('*',_check) = 0) then
if (Pos('?',_check) = 0) then
if (Pos('#',_check) = 0) then
result:=-1
else result:=Pos('#',_check)
else result:=Pos('?',_check)
else result:=Pos('*',_check);
end;
function parse_from_wc(const wildcard, _check:string):boolean;
var next_wc,off_set,first_of:integer;
t,tmp:string;
begin
result:=true;
if (copy(wildcard,1,1) = '#') then begin
if (length(_check) = 0) or (not IsNumber(copy(_check,1,1))) then begin
result:=false;
exit;
end;
t:=copy(wildcard,2,length(wildcard));
next_wc:=find_wildcard(t);
if (next_wc = -1) then
result:=true
else begin
tmp:=copy(wildcard,2,next_wc-1);
if (length(tmp) = 0) then begin
result:=false;
exit;
end else
if (IsNumber(copy(_check,1,length(tmp)))) then
if (not parse_from_wc(copy(wildcard,next_wc+1,length(wildcard)),copy(_check,next_wc+1,length(_check)))) then begin
result:=false;
exit;
end else
else begin
result:=false;
exit;
end;
end;
end else if (copy(wildcard,1,1) = '?') then begin
if (length(_check) = 0) then begin
result:=false;
exit;
end;
t:=copy(wildcard,2,length(wildcard));
next_wc:=find_wildcard(t);
if (next_wc = -1) then
result:=(t = copy(_check,2,length(_check)))
else begin
tmp:=copy(wildcard,2,next_wc-1);
if (length(tmp) = 0) then begin
result:=false;
exit;
end else
if (not parse_from_wc(copy(wildcard,next_wc+1,length(wildcard)),copy(_check,next_wc+1,length(_check)))) then begin
result:=false;
exit;
end;
end;
end else if (copy(wildcard,1,1) = '*') then begin
t:=copy(wildcard,2,length(wildcard));
next_wc:=find_wildcard(t);
if (next_wc = -1) then begin
if (length(_check) >= length(t)) then
result:=(copy(_check,1+length(_check)-length(t),length(_check)) = t)
else result:=false;
exit;
end else begin
if (next_wc = 0) then begin
result:=parse_from_wc(copy(wildcard,2,length(wildcard)),_check);
exit;
end else begin
off_set:=1;
tmp:=copy(wildcard,2,next_wc-1);
while (true) do begin
first_of:=Pos(tmp,copy(_check,off_set,length(_check)));
if (first_of = 0) then begin
result:=false;
exit;
end else begin
off_set:=first_of+length(tmp);
if (parse_from_wc(copy(wildcard,next_wc+2,length(wildcard)),
copy(_check,off_set,length(_check)))) then begin
result:=true;
exit;
end;
end;
end;
end;
end;
end;
end;
begin
if (length(check) = 0) then begin
result:=false;
exit;
end;
result:=true;
_next_wc:=find_wildcard(mask);
if (_next_wc = 0) then
result:=parse_from_wc(mask,check)
else if (_next_wc = -1) then
result:=(check = mask)
else begin
a:=copy(check,1,_next_wc-1);
b:=copy(mask,1,_next_wc-1);
if (a <> b) then begin
result:=false;
exit;
end;
if (not parse_from_wc(copy(mask,_next_wc,length(mask)-_next_wc+1),
copy(check,_next_wc,length(check)-_next_wc+1))) then begin
result:=false;
exit;
end;
end;
end;
Edit: Folgende Funktion wird auch benötigt:
Delphi-Quellcode:
function IsNumber(const s:string): Boolean;
var c:integer;
const nums:string = '0123456789';
begin
result:=(length(s) > 0);
for c:=1 to length(s) do
if (Pos(copy(s,c,1),nums) = 0) then begin
result:=false;
Break;
end;
end;
Viel Spaß
cu
|