Advertisement
Janilabo

done

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