• 网络学院
  • IT资讯
  • 操作系统
  • 网络技术
  • 软件应用
  • 办公软件
  • 编程技术
  • 网站架设
  • 数据库类
  • 平面设计
  • 多媒体类
  • 游戏资讯
  • 教学论文
  • 认证考试
Delphi学习:2个不错的通配符比较函数
  站点:
  • 首 页
  • 最新软件
  • 文章教程
  • 国内软件
  • 国外软件
  • 绿色软件
  • 源码下载
  • 字体下载
Delphi学习:2个不错的通配符比较函数
软件发布 Delphi学习:2个不错的通配符比较函数
网络软件 系统工具 应用软件 联络聊天 图形图像 多媒体类 行业软件 游戏娱乐 编程开发 安全相关 教育教学 数码软件 绿软下载
热门软件: QQ 瑞星 pplive e话通 木马克星 千千静听 office2000 五笔字根 Photoshop 视频分割
返回文章教程首页 >> 编程技术 >> Delphi教程 >> Delphi学习:2个不错的通配符比较函数

Delphi学习:2个不错的通配符比较函数

添加时间: 2006-2-25 5:41:25  作者: Delphi教程  阅读次数:66   来源: http://www.d9soft.com

       

  近日在和朋友讨论 MaskMatch 时偶得2个不错的算法。
  函数1 只支持'*','?'模糊匹配。速度比采用递归算法的快近2倍,比TMask方法快很多。
  函数2 完全支持正规表达式。速度于之前的相同。(不会正规表达式的朋友慎用)



  // ===========================
  // Function 1
  // ===========================

  // Check if the string can match the wildcard. It can be used for unicode strings as well!
  // C: 2004-07-24 M: 2004-07-24
  function MaskMatch(const aPattern, aSource: string): Boolean;
  var
  StringPtr, PatternPtr: PChar;
  StringRes, PatternRes: PChar;
  begin
  Result := False;
  StringPtr := PChar(UpperCase(aSource));
  PatternPtr := PChar(UpperCase(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 2
  // ===========================

  function _MatchPattern(aPattern, aSource: PChar): Boolean;
  begin
  Result := True;
  while (True) do
  begin
  case aPattern[0] of
  #0 : begin
  //End of pattern reached.
  Result := (aSource[0] = #0); //TRUE if end of aSource.
  Exit;
  end;

  '*': begin //Match zero or more occurances of any char.
  if (aPattern[1] = #0) then
  begin
  //Match any number of trailing chars.
  Result := True;
  Exit;
  end else
  Inc(aPattern);

  while (aSource[0] <> #0) do
  begin
  //Try to match any substring of aSource.
  if (_MatchPattern(aSource, aPattern)) then
  begin
  Result := True;
  Exit;
  end;

  //Continue testing next char...
  Inc(aSource);
  end;
  end;

  '?': begin //Match any one char.
  if (aSource[0] = #0) then
  begin
  Result := False;
  Exit;
  end;


  //Continue testing next char...
  Inc(aSource);
  Inc(aPattern);
  end;

  '[': begin //Match given set of chars.
  if (aPattern[1] in [#0,'[',']']) then
  begin
  //Invalid Set - So no match.
  Result := False;
  Exit;
  end;

  if (aPattern[1] = '^') then
  begin
  //Match for exclusion of given set...
  Inc(aPattern, 2);
  Result := True;
  while (aPattern[0] <> ']') do
  begin
  if (aPattern[1] = '-') then
  begin
  //Match char exclusion range.
  if (aSource[0] >= aPattern[0]) and (aSource[0] <= aPattern[2]) then
  begin
  //Given char failed set exclusion range.
  Result := False;
  Break;
  end else
  Inc(aPattern, 3);
  end else
  begin
  //Match individual char exclusion.
  if (aSource[0] = aPattern[0]) then
  begin
  //Given char failed set element exclusion.
  Result := False;
  Break;
  end else


  Inc(aPattern);
  end;
  end;
  end else
  begin
  //Match for inclusion of given set...
  Inc(aPattern);
  Result := False;
  while (aPattern[0] <> ']') do
  begin
  if (aPattern[1] = '-') then
  begin
  //Match char inclusion range.
  if (aSource[0] >= aPattern[0]) and (aSource[0] <= aPattern[2]) then
  begin
  //Given char matched set range inclusion.
  // Continue testing...
  Result := True;
  Break;
  end else
  Inc(aPattern, 3);
  end else
  begin
  //Match individual char inclusion.
  if (aSource[0] = aPattern[0]) then
  begin
  //Given char matched set element inclusion.
  // Continue testing...
  Result := True;
  Break;
  end else
  Inc(aPattern);
  end;
  end;
  end;

  if (Result) then
  begin
  //Match was found. Continue further.
  Inc(aSource);
  //Position Pattern to char after "]"

 


  while (aPattern[0] <> ']') and (aPattern[0] <> #0) do Inc(aPattern);
  if (aPattern[0] = #0) then
  begin
  //Invalid Pattern - missing "]"
  Result := False;
  Exit;
  end else
  Inc(aPattern);
  end else
  Exit;
  end;

  else begin //Match given single char.
  if (aSource[0] <> aPattern[0]) then
  begin
  Result := False;
  Break;
  end;

  //Continue testing next char...
  Inc(aSource);
  Inc(aPattern);
  end;
  end;
  end;
  end;

  function MatchPattern(const aPattern, aSource: string): Boolean;
  begin
  Result := _MatchPattern(PChar(aPattern), PChar(aSource));
  end;

 

上下文章:

 

上一篇文章: Delphi控制Excel的重要属性和方法 下一篇文章: 字符串分割扩展 SplitEx

相关文章:

  • 一套很不错的百度Hi“有啊表情
  • 11款非常不错 foobar2000 皮肤
  • 学习Ruby-Web程序开发的10条理由
  • 通配符*帮我分类背单词 -《疯狂单词》使用技巧
  • Word2007直观比较两个文档的内容

相关软件:

  • Excel比较大师 0.09
  • Turbo C for Windows 集成实验与学习环境 V6.11
  • InfoPower for Delphi 5 V2000.17
  • 汇编语言学习工具 3.6
  • EnglishField英语学习软件 7.8
  • 限时英语(初中英语学习软件) 6.4

 

快速导航

  • 网络学院
  • 精品汇聚
  • 字体下载
  • 教程下载
  • ASP源码
  • PHP源码
  • Net源码
  • JSP 源码

编程技术分类导航

  • ASP & ASP.NET教程
  • PHP教程
  • JSP教程
  • C/C++教程
  • VB & VB.NET教程
  • VC教程
  • Delphi教程
  • BCB教程
  • VFP教程
  • PB教程
  • JAVA教程
  • XML教程
  • C#教程
  • CGI教程

本类经典文章推荐

  • Delphi的两个实用技巧(1)播放Flash
  • Delphi的两个实用技巧(2)巧用Wind...
  • delphi实例编程之--制作可随处拖放...
  • 关于VisiBroker For Delphi的使用...
  • 关于VisiBroker For Delphi的使用...
  • 关于VisiBroker For Delphi的使用...
  • 在线播放器DIY
  • Delphi让你发送Flash电子邮件
  • 在窗口标题区添加按钮
  • 用Delphi 6编程实现自动标注汉语拼...

Delphi教程阅读排行

  • Delphi7从入门到精通之历数Delphi...
  • Delphi的两个实用技巧(1)播放Flash
  • Delphi7从入门到精通之认识Delphi...
  • delphi实例编程之--制作可随处拖放...
  • Delphi控件,我们也可以(1)
  • Delphi的两个实用技巧(2)巧用Wind...
  • Spcomm串口控件的例程
  • 在线播放器DIY
  • Delphi控件,我们也可以(2)
  • 用Delphi + DirectX开发简单RPG游...

编程技术阅读总排行

  • VB入门教程之一
  • Java连接数据库实例
  • VC++之List Box/Check List Box控...
  • 第二章 PowerBuilder 入门之创建新...
  • VC++ List Ctrl控件
  • 第一章 什么是PowerBuilder
  • VC++ Combo Box/Combo Box Ex控件
  • 学C++不得不看的一篇文章
  • VB入门教程之二
  • VC++之Button控件

广告位置

字母检索 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 回到顶部

关于我们 | 版权声明 | 免责条款 | 广告联系 | 软件发布 | 下载帮助 | 下载排行 | 网站地图 | 特别鸣谢 | 友情连接

copyright; 2005-2008 D9soft.com 第九软件网 版权所有