设为首页】 【加入收藏】 【网站地图】 【商品折扣
娱乐一生 娱乐明星
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
首页  |  java  |  .NET  |  C/C++  |  网页技术  |  php  |  asp  |  delphi  |  VC  |  VB开发  |  游戏开发  |  软件工程  |  Power Builder  |  Linux开发  |  Windows开发技巧
当前位置:首页 >> delphi >> 2个不错的通配符比较函数_Delphi教程_src119.com

2个不错的通配符比较函数_Delphi教程_src119.com -

近日在和朋友讨论 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(co t aPattern, aSource: string): Boolea 
var
  StringPtr, Patter tr: PChar;
  StringRes, PatternRes: PChar;
begin
  Result := False;
  StringPtr := PChar(U erCase(aSource));
  Patter tr := PChar(U erCase(aPattern));
  StringRes := nil;
  PatternRes := nil;
  repeat
    repeat // ohne vorangegangenes "*"
      case Patter tr^ of
        #0 : begin
               Result := StringPtr^ = #0;
               if Result or (StringRes = nil) or (PatternRes = nil) then Exit;
               tringPtr := StringRe 
               atter tr := PatternRe 
               reak;
             end;
        '*': begin
               Inc(Patter tr);
               atternRes := Patter tr;
               reak;
             end;
        '?': begin
               if StringPtr^ = #0 then Exit;
               Inc(StringPtr);
               Inc(Patter tr);
             end;
        else begin
               if StringPtr^ = #0 then Exit;
               if StringPtr^ < gt; Patter tr^ then
               egin
                 if (StringRes = nil) or (PatternRes = nil) then Exit;
                 tringPtr := StringRe 
                 atter tr := PatternRe 
                 reak;
               end else
               egin
                 Inc(StringPtr);
                 Inc(Patter tr);
               end;
             end;
      end;
    until False;

    repeat // mit vorangegangenem "*"
      case Patter tr^ of
        #0 : begin
               Result := True;
               Exit;
             end;
        '*': begin
               Inc(Patter tr);
               atternRes := Patter tr;
             end;
        '?': begin
               if StringPtr^ = #0 then Exit;
               Inc(StringPtr);
               Inc(Patter tr);
             end;
        else begin
               repeat
                 if StringPtr^ = #0 then Exit;
                 if StringPtr^ = Patter tr^ then Break;
                 Inc(StringPtr);
               until False;
               Inc(StringPtr);
               tringRes := StringPtr;
               Inc(Patter tr);
               reak;
             end;
      end;
    until False;
  until False;
end;


// ===========================
// Funtion 2
// ===========================

function _MatchPattern(aPattern, aSource: PChar): Boolea 
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
             egin
               //Match any number of trailing chars.
               Result := True;
               Exit;
             end else
              Inc(aPattern);

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

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

      '?': begin //Match any one char.
             if (aSource[0] = #0) then
             egin
               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
             egin
               //Invalid Set - So no match.
               Result := False;
               Exit;
             end;

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

             if (Result) then
             egin
               //Match was found. Continue further.
               Inc(aSource);
               //Position Pattern to char after "]"
               while (aPattern[0] < gt; ']') and (aPattern[0] < gt; #0) do Inc(aPattern);
               if (aPattern[0] = #0) then
               egin
                 //Invalid Pattern - mi ing "]"
                 Result := False;
                 Exit;
               end else
                 Inc(aPattern);
             end else
               Exit;
           end;

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

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

function MatchPattern(co t aPattern, aSource: string): Boolea 
begin
  Result := _MatchPattern(PChar(aPattern), PChar(aSource));
end;



 

娱乐图摘

更多 >>

靓丽清纯美女meimei

美女私房全裸照
导演劝女演员脱衣服(视频)

大胆火辣人体艺术写真(图)

黑丝妹妹热辣诱惑-丝袜美女妹妹

PLMM 漂亮妹妹图集-妹妹图库

全球美女图库-美女集中营

52MM 我爱漂亮妹妹-制服妹妹诱惑

图王图库-世界美女明星图片资料库
美女写真集锦

激情两性-解密性生活
浴室MM湿身内衣诱惑
邻家小妹洗澡被偷拍(视频)

热点文章

更多

· 使用MIDAS访问远程Access数据库_Delphi教程_
· Delphi2005学习笔记4——再谈NameSpace和D
· 多层数据库开发十二:使用数据控件_Delphi教程_src1
· 在Delphi中自己建立交叉表_Delphi教程_src11
· 用Delphi设计代理服务器_Delphi教程_src119
· Delphi开发单机瘦数据库程序要点_Delphi教程_sr
· 拖拉打开文件_Delphi教程_src119.com
· 搜索字符串在流中的位置_Delphi教程_src119.co
· 用Delphi编写Win2000服务程序_Delphi教程_
· 創建一個簡單的"專家"_Delphi教程

热点文章

更多