Kig på følgende, en simpel funktion der også lader dig søge på '*llica*' og '?llica*' og den slags:
unit Wildcard;
interface
uses
SysUtils;
function WildcardMatch(const source, pattern: String): Boolean;
function WildcardMatchIC(const source, pattern: String): Boolean;
function WildcardMatchEx(const source, pattern: String; const ic: Boolean): Boolean;
implementation
function WildcardMatchEx(const source, pattern: String; const ic: Boolean): Boolean;
begin
{ perform a wildcard match, either case-sensitive or case-insensitive,
depending on the ic flag. }
if ic = True then
result := WildcardMatchIC(source, pattern)
else
result := WildcardMatch(source, pattern);
end;
function WildcardMatchIC(const source, pattern: String): Boolean;
begin
{ perform a case insensitive wildcard match by converting the pattern
and source strings to uppercase. }
result := WildcardMatch(UpperCase(source), UpperCase(pattern));
end;
function WildcardMatch(const source, pattern: String): Boolean;
var
pSource: Array [0..255] of Char;
pPattern: Array [0..255] of Char;
function MatchPattern(element, pattern: PChar): Boolean;
function IsPatternWild(pattern: PChar): Boolean;
begin
Result := StrScan(pattern,'*') <> nil;
if not Result then Result := StrScan(pattern,'?') <> nil;
end;
begin
if 0 = StrComp(pattern,'*') then
Result := True
else if (element^ = Chr(0)) and (pattern^ <> Chr(0)) then
Result := False
else if element^ = Chr(0) then
Result := True
else begin
case pattern^ of
'*': if MatchPattern(element,@pattern[1]) then
Result := True
else
Result := MatchPattern(@element[1],pattern);
'?': Result := MatchPattern(@element[1],@pattern[1]);
else
if element^ = pattern^ then
Result := MatchPattern(@element[1],@pattern[1])
else
Result := False;
end;
end;
end;
begin
StrPCopy(pSource,source);
StrPCopy(pPattern,pattern);
Result := MatchPattern(pSource,pPattern);
end;
end.
Ref.:
http://www.eksperten.dk/spm/285290