网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Delphi
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.用Delphi编写CGI程序(六).
.在delphi中,如何把MDI工作区的粗.
.CRC算法的实现.
.delphi7从入门到精通之(一).
.如何使程序在运行时自动注册Acti.
.如何制作平面式列头的Listview.
.Delphi编写组件封装asp代码的基本.
.用Delphi4.0直接控制Word97.
.一个可以靠右显示的简单Edit控件.
.取Run下所有值(原创).
.用Ehlib二次开发报表打印程序,实.
.从Internet时间服务器获取标准格.
.Delphi数据库编程教程(二).
.用Delphi编写Win2000服务程序.
.设计模式、用Delphi实现---->Str.
.VCL中消息处理初探.
.Win32调试API学习心得(一).
.远程控制篇:抓取远程屏幕图像.
.delphi中的split函数.
.Delphi中的包(三):bpl和dll.

2个不错的通配符比较函数

发表日期:2006-2-4


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



// ===========================
// Funtion 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;


// ===========================
// Funtion 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;

上一篇:如何从MySQL数据库表中检索数据 人气:3744
下一篇:Delphi中的DLL封装和调用对象编写碰到的问题 人气:4723
浏览全部Delphi的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐