Advertisement
Janilabo

done2

Dec 4th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.15 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.               begin  
  60.                 _right := LastPos(TSA[h], Str);
  61.                 p := _right;
  62.                 for i := (h - 1) downto s do
  63.                 begin
  64.                   p := LastPosEx(TSA[i], Str, (p - 1));
  65.                   if (p < 1) then
  66.                     Exit;
  67.                 end;    
  68.               end;  
  69.             end;      
  70.             case (b and e) of
  71.               False:
  72.               if e then                    
  73.                 Match := Copy(Str, _left, ((l - _left) + 1))
  74.               else    
  75.                 if Sensitive then  
  76.                   Match := Copy(Str, _left, (_right - _left))
  77.                 else
  78.                   Match := Copy(Str, _left, ((_right - _left) + Length(TSA[h])));
  79.               True: Match := Copy(Str, 1, l);
  80.             end;
  81.             Result := True;      
  82.           end;          
  83.         end;    
  84.         True:
  85.         begin      
  86.           p := Pos(TSA[0], Str);
  87.           Result := (p > 0);
  88.           if Result then
  89.           case b of
  90.             True:
  91.             case e of
  92.               False: Match := Copy(Str, 1, ((t + p) - 1));
  93.               True: Match := Copy(Str, 1, l);
  94.             end;
  95.             False:
  96.             case e of
  97.               False: Match := Copy(Str, p, t);
  98.               True: Match := Copy(Str, p, ((l - p) + t));
  99.             end;      
  100.           end;
  101.         end;
  102.       end;
  103.     end;
  104.   end;
  105. end;
  106.  
  107. var
  108.   Str, expression, Match: string;
  109.  
  110. begin
  111.   ClearDebug;
  112.   expression := '*is * * text';
  113.   Str := 'This is text textyyyyyyyrt';
  114.   if StrMatches(expression, Str, '*', True, Match) then
  115.     WriteLn(Match);
  116. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement