Mega Code Archive

 
Categories / Delphi / String
 

String Pattern Matching

Title: String Pattern Matching Question: Use it when you want to compare strings using '*' or '?' operator Answer: String Pattern Matching The islike function take 2 strings and compare them using simple pattern matching. Only '*' and '?' are implemented, range syntaxe '[]' are not implemented. Exemple : islike('abcdxabcdxabcdefg', '*abcd?abcdefg*') = true, islike('abc*defg', 'abc*efg') = true , islike('abaababcac', '*abaabcac*') = false , etc... Maybe this is faster than the MatchesMask function and the another recursive function which is translated from MatchPattern() of common.c in MSDN Samples\VC98\sdk\sdktools\tlist . . Function IsLike(const ABC, AX:string) :boolean; Var axNext,nextval:array of integer; AbcLen,AxLen,AbcI,AxI,AxWWW,AxStar:integer; Begin//aaa Result:= true; abclen:=length(abc); axlen:=length(ax) ; setlength(axnext,axlen+1); setlength(nextval,axlen+1); abci:=1; axi:=1; axstar:=0; axwww:=0; axnext[1]:=0; nextval[1]:=0; while ((abcibegin while(axi begin//eeeeeeee if ax[axi]='*' then begin nextval[axi+1]:=axi;axnext[axi+1]:=axi;axwww:=axi;break; end; if (ax[axwww]='*')or(ax[axi]=ax[axwww])or(ax[axwww]='?')or(axwww=0)then begin axwww:=axwww+1;axnext[axi+1]:=axwww; if ax[axi+1]ax[axwww] then nextval[axi+1]:=axwww else nextval[axi+1]:=nextval[axwww]; break; end else begin axwww:=nextval[axwww]; end; end;//eeeeeeee if(ax[axi]='*') then begin //xxx axstar:=axi;axi:=axi+1;continue; end;//xxx if(ax[axi]= '?') then begin abci:=abci+1; axi:=axi+1;continue; end; if(ax[axi]=abc[abci])then begin inc(abci);inc(axi);continue; end; if(ax[axi]abc[abci])then begin//ccc axi:=axnext[axi]; if axi=0 then axwww:=0 else axwww:= axnext[axi]; if (axi=axstar) and (axstar=1) then begin inc(abci); continue; end; if (axi=axstar) and (axstar=0) then begin result:=false; break; end; end;//ccc end;//main if (result=true)and (abciabclen) and(axiif (result=true)and (abciaxlen) and (ax[axlen]'*') then result:=false; end;//aaa author: JunYu . Li ,a chinese , email: e271828@163.net,msSQLserver2000@163.com