Advertisement
Janilabo

Untitled

Dec 3rd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.70 KB | None | 0 0
  1. function StrMatches(Expr, Str: string; Ignore: Char; Sensitive: Boolean; var Match: String): Boolean;
  2. var
  3.   TSA: TStrArray;
  4.   Hi, i, _Right, P, Left, r, s: Integer;
  5.   b, e: Boolean;
  6. begin
  7.   Match := '';
  8.   Result := False;
  9.   _Right := Length(Expr);
  10.   if ((_Right > 0) and not (_Right > Length(Str))) then
  11.   begin
  12.     TSA := Explode(ignore, expr);
  13.     Hi := High(TSA);
  14.     r := 0;
  15.     for i := 0 to Hi do
  16.       if (Length(TSA[i]) > 0) then
  17.       begin
  18.         TSA[r] := TSA[i];
  19.         r := (r + 1);
  20.       end;    
  21.     SetLength(TSA, r);
  22.     Hi := High(TSA);
  23.     b := (expr[1] = ignore);
  24.     e := (expr[_Right] = ignore);
  25.     if (Hi > -1) then              
  26.     case (Hi = 0) of
  27.       False:
  28.       begin    
  29.         case b of
  30.           True:
  31.           begin
  32.             Left := 1;  
  33.             _Right := Left;
  34.             s := 0;
  35.           end;
  36.           False:
  37.           begin
  38.             Left := Pos(TSA[0], Str);
  39.             _Right := (Left + Length(TSA[0]));
  40.             s := 1;
  41.           end;
  42.         end;
  43.         if ((Hi > (s - 1)) and (Left > 0)) then
  44.         begin
  45.           case Sensitive of
  46.             True:
  47.               for i := s to Hi do
  48.               begin
  49.                 p := PosEx(TSA[i], Str, _Right);
  50.                 if (p > 0) then
  51.                   _Right := (P + Length(TSA[i]))
  52.                 else
  53.                   Exit;
  54.               end;
  55.             False:
  56.               for i := s to Hi do
  57.               begin
  58.                 p := LastPos(TSA[i], str);
  59.                 if (p > _Right) then
  60.                   _Right := (P + Length(TSA[i]))
  61.                 else
  62.                   Exit;
  63.               end;  
  64.           end;    
  65.           if e then                    
  66.             Match := Copy(Str, Left, ((Length(str) - Left) + 1))
  67.           else
  68.             Match := Copy(Str, Left, (_Right - Left));
  69.           Result := True;      
  70.         end;          
  71.       end;    
  72.       True:
  73.       begin      
  74.         P := Pos(TSA[0], Str);
  75.         Result := (P > 0);
  76.         if Result then
  77.         case b of
  78.           True:
  79.           case e of
  80.             False: match := Copy(Str, 1, ((Length(TSA[0]) + P) - 1));
  81.             True: match := Copy(Str, 1, Length(Str));
  82.           end;
  83.           False:
  84.           case e of
  85.             False: match := Copy(Str, P, Length(TSA[0]));
  86.             True: match := Copy(Str, P, Length(Str) - Length(TSA[0]));
  87.           end;      
  88.         end;
  89.       end;
  90.     end;
  91.   end;
  92. end;
  93.  
  94. var
  95.   str, expression, match: string;
  96.  
  97. begin
  98.   ClearDebug;
  99.   expression := 'Test*it*work*';
  100.   str := 'This is Test,             it works!';
  101.   if StrMatches(expression, str, '*', True, match) then
  102.     WriteLn(match);
  103. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement