filefilter:= 'notepad.*';//通配符处理 filehead:= copy(1,pos('*',filefilter) -1); filetail:= ...;//得到带通配符的字串的尾 Err:=FindFirst('*.*',$37,Sr) ; While (Err = 0) do begin if Sr.Name[1]<>'.' then begin Filename:=ExpandFileName(Sr.Name); if pos(filehead,Filename)>0 and pos(filetail,filename)>0 then begin .......//处理罗 end; end;
procedure myfindfile(dir: String); var filename: String; FileInfo: TSearchRec; AttrWord: Word; FileExt: string; begin AttrWord := DDL_READWRITE or faDirectory; filename := dir + '\com*.*m'; if FindFirst(filename, AttrWord, FileInfo) = 0 then begin repeat //文件找到啦! 在FileInfo中 until FindNext(FileInfo) <> 0; FindClose(FileInfo); end; end;
初步测试正确: 比如isinclude('com*.*m','editcommand.comdf');返回truefunction isinclude(sub,str:string):boolean; var i,j,count1,count2:integer; head,tmp:string; begin count1:= length(str); count2:= length(sub); if count1< count2 then result:= false else repeat i:= pos('*',sub); if i>0 then head:=copy(sub,1,i -1) else head:= sub; j:= pos(head,str); if j<=0 then begin result:= false; exit; end; delete(str,1,j+length(head)); delete(sub,1,i); until i<=0; if i<0 then result:= true; end;
搜索文件的API本身支持通配符!不需要那么麻烦的!如果你硬是要算法: function Matchstrings(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; var t : Integer; 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;procedure TForm1.Button1Click(Sender: TObject); begin if Matchstrings('Sean Stanley', '?ean *n*') then ShowMessage('strings match!'); if Matchstrings('Sean', 'Se?n') then ShowMessage('strings match!'); if not Matchstrings('Sean', 'Se?nn') then ShowMessage('strings don''t match!'); end;
注意,他是Whole Match德,因此若需要匹配不完整的,请在最后面添加*
var: String str1,str2; Integer n1,n2; Boolean ok; str1:='autoexec.bat';str2:='a*t*e*e*.*t'; n2:=1; ok:=false; for n1:=1 to str1.length() do begin if str1[n1]=str2[n2] then begin n2:=n2+1; n1:=n1+1; end; if n2>str2.length() then begin ok=true; break; end; //这里返回最后结果 if str2[n2]='*' then n2:=n2+1; n1:=n1+1; end;
filehead:= copy(1,pos('*',filefilter) -1);
filetail:= ...;//得到带通配符的字串的尾
Err:=FindFirst('*.*',$37,Sr) ;
While (Err = 0) do
begin
if Sr.Name[1]<>'.' then
begin
Filename:=ExpandFileName(Sr.Name);
if pos(filehead,Filename)>0 and pos(filetail,filename)>0 then
begin
.......//处理罗
end;
end;
如com*. 从command的n开始找。直到找到.为止,找不到就不匹配喽。如果*结尾。只要保证源串后面还有东东就ok了。第一次在星星面前发东西。仰视中
兄台怎么知道我喝酒了,莫不是
.......
//上面为遍历文件夹,取文件名
1。将要查找的format按*分解到一动态数组
2。用一循环对文件名进行字符串匹配,用pos判断数组的第一个元素是否在文件名中出现,否,查找下一个文件,是,判断下一个数组元素
3。若我所要的串都在文件名中,判断顺序是否正确。
4。判断顺序,可以取出所有的数组元素的pos,判断是否第一个元素的值最小,是,将第一个元素的pos+length前的字符去掉,再重复上面的操作,可以用递归或循环。
5。所有条件都符合,yes
var filename: String;
FileInfo: TSearchRec; AttrWord: Word; FileExt: string;
begin
AttrWord := DDL_READWRITE or faDirectory;
filename := dir + '\com*.*m';
if FindFirst(filename, AttrWord, FileInfo) = 0 then begin
repeat
//文件找到啦! 在FileInfo中
until
FindNext(FileInfo) <> 0;
FindClose(FileInfo);
end;
end;
比如isinclude('com*.*m','editcommand.comdf');返回truefunction isinclude(sub,str:string):boolean;
var
i,j,count1,count2:integer;
head,tmp:string;
begin
count1:= length(str);
count2:= length(sub);
if count1< count2 then
result:= false
else
repeat
i:= pos('*',sub);
if i>0 then head:=copy(sub,1,i -1) else head:= sub;
j:= pos(head,str);
if j<=0 then
begin
result:= false;
exit;
end;
delete(str,1,j+length(head));
delete(sub,1,i);
until i<=0;
if i<0 then result:= true;
end;
function Matchstrings(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;
var
t : Integer;
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;procedure TForm1.Button1Click(Sender: TObject);
begin
if Matchstrings('Sean Stanley', '?ean *n*') then ShowMessage('strings match!');
if Matchstrings('Sean', 'Se?n') then ShowMessage('strings match!');
if not Matchstrings('Sean', 'Se?nn') then ShowMessage('strings don''t match!');
end;
str1:='autoexec.bat';str2:='a*t*e*e*.*t'; n2:=1;
ok:=false;
for n1:=1 to str1.length() do
begin
if str1[n1]=str2[n2] then
begin n2:=n2+1; n1:=n1+1; end;
if n2>str2.length() then
begin ok=true; break; end; //这里返回最后结果
if str2[n2]='*' then n2:=n2+1;
n1:=n1+1;
end;