如果进行字符串的模糊匹配,比如文件名的匹配。(200)

  • 主题发起人 主题发起人 yanghaijun
  • 开始时间 开始时间
Y

yanghaijun

Unregistered / Unconfirmed
GUEST, unregistred user!
哪位大侠有相关的函数、类或心得,请不吝赐教。比如 winabc.sys 匹配 win*.sys 或 win???.sys
 
正则表达式应该可以解决你的问题吧?!http://unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm
 
unit uMaskMatch;interfaceuses SysUtils, Classes;function MatchFileName(const AFileName, APatterns: string): Boolean;implementationfunction MaskMatch(const APattern, ASource: string): Boolean;var StringPtr, PatternPtr: PChar
StringRes, PatternRes: PChar;begin Result := False
StringPtr := PChar(LowerCase(ASource))
PatternPtr := PChar(LowerCase(APattern))
StringRes := nil
PatternRes := nil
repeat repeat // ohne vorangegangenes "*" case PatternPtr^ of #0: begin Result := StringPtr^ = #0
if Result or (StringRes = nil) or (PatternRes = nil) then Exit
StringPtr := StringRes
PatternPtr := PatternRes
Break
end
'*': begin Inc(PatternPtr)
PatternRes := PatternPtr
Break
end
'?': begin if StringPtr^ = #0 then Exit
Inc(StringPtr)
Inc(PatternPtr)
end
else begin if StringPtr^ = #0 then Exit
if StringPtr^ <> PatternPtr^ then begin if (StringRes = nil) or (PatternRes = nil) then Exit
StringPtr := StringRes
PatternPtr := PatternRes
Break
end else begin Inc(StringPtr)
Inc(PatternPtr)
end
end
end
until False
repeat // mit vorangegangenem "*" case PatternPtr^ of #0: begin Result := True
Exit
end
'*': begin Inc(PatternPtr)
PatternRes := PatternPtr
end
'?': begin if StringPtr^ = #0 then Exit
Inc(StringPtr)
Inc(PatternPtr)
end
else begin repeat if StringPtr^ = #0 then Exit
if StringPtr^ = PatternPtr^ then Break
Inc(StringPtr)
until False
Inc(StringPtr)
StringRes := StringPtr
Inc(PatternPtr)
Break
end
end
until False
until False;end;function MatchFileName(const AFileName, APatterns: string): Boolean;var i : Integer
lst : TStrings;begin Result := False
lst := TStringList.Create
try ExtractStrings([';'], [], PChar(APatterns), lst)
for i := 0 to lst.Count - 1 do if MaskMatch(lst, AFileName) then begin Result := True
break
end
finally lst.Free
end;end;end.
 
谢谢两位,ranyang 给的文章很好,轻舞肥羊的 unit 很好用,多谢谢两位的关注。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部