Advertisement
Janilabo

mask match

Dec 4th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.97 KB | None | 0 0
  1. function StrMaskMatch(expression, str: string; mask: Char; var match: string): Boolean;
  2. var
  3.   a: TStrArray;
  4.   s, h, i, l, w, z, f, p, o, m: Integer;  
  5.   e: string;  
  6.   x, y: Boolean;
  7. begin
  8.   match := '';
  9.   Result := False;
  10.   l := Length(expression);
  11.   z := Length(str);
  12.   if ((l > 0) and (z > 0)) then
  13.   begin
  14.     l := (l - 1);
  15.     for i := 1 to l do
  16.       if not ((expression[i] = mask) and (expression[(i + 1)] = mask)) then
  17.         e := (e + expression[i]);
  18.     e := (e + expression[(l + 1)]);  
  19.     a := Explode(mask, e);
  20.     h := High(a);    
  21.     s := 0;      
  22.     w := 0;
  23.     for i := 0 to h do
  24.       if (a[i] <> '') then
  25.       begin
  26.         a[s] := a[i];
  27.         w := (w + Length(a[s]));
  28.         s := (s + 1);
  29.       end;
  30.     if (s > 0) then
  31.     begin  
  32.       SetLength(a, s);
  33.       h := (s - 1);
  34.       s := Length(e);
  35.       x := (e[1] = mask);
  36.       y := (e[s] = mask);
  37.       if ((x or y) and (w > z)) then
  38.         Exit;  
  39.       case x of
  40.         True:
  41.         begin
  42.           f := 2;
  43.           m := 1;
  44.         end;
  45.         False:
  46.         begin
  47.           f := 1;
  48.           m := z;
  49.         end;
  50.       end;  
  51.       p := f;  
  52.       o := 0;
  53.       for i := 0 to h do
  54.       begin
  55.         p := PosEx(a[i], str, p);
  56.         if (p < 1) then
  57.           Exit;
  58.         if (p < m) then
  59.           m := p;  
  60.         p := ((p + Length(a[i])) + 1);
  61.       end;
  62.       Result := not y;  
  63.       case Result of
  64.         False:
  65.         begin                                  
  66.           Result := ((p - 1) <= z);
  67.           if Result then
  68.             match := Copy(str, m, ((z - m) + 1));
  69.         end;
  70.         True: match := Copy(str, m, (p - m) - 1);
  71.       end;
  72.     end;
  73.   end;
  74. end;
  75.  
  76. var
  77.   Str, expression, Match: string;
  78.  
  79. begin
  80.   ClearDebug;
  81.   expression := '********his is * text right here!*';
  82.   Str := 'This is text text right here!';
  83.   if StrMaskMatch(expression, Str, '*', Match) then
  84.     WriteLn('Matched: "' + Match + '"');
  85. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement