Advertisement
Janilabo

version2

Dec 4th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 12.45 KB | None | 0 0
  1. function StrMaskMatch(expression, str: string; mask: Char; Sensitive: Boolean; 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.     Result := (s = 0);
  31.     case Result of
  32.       False:
  33.       begin      
  34.         x := (e[1] = mask);
  35.         y := (e[Length(e)] = mask);
  36.         case (s > 1) of
  37.           True:
  38.           begin  
  39.             SetLength(a, s);
  40.             h := (s - 1);  
  41.             s := Length(e);
  42.             if ((x or y) and (w > z)) then
  43.               Exit;
  44.             case Sensitive of
  45.               True:
  46.               begin
  47.                 case x of
  48.                   True:
  49.                   begin
  50.                     f := 2;
  51.                     m := 1;
  52.                   end;
  53.                   False:
  54.                   begin
  55.                     f := 1;
  56.                     m := z;
  57.                   end;
  58.                 end;  
  59.                 p := f;  
  60.                 o := 0;
  61.                 for i := 0 to h do
  62.                 begin
  63.                   p := PosEx(a[i], str, p);
  64.                   if (p < 1) then
  65.                     Exit;
  66.                   if (p < m) then
  67.                     m := p;  
  68.                   p := ((p + Length(a[i])) + 1);
  69.                 end;
  70.                 Result := not y;  
  71.                 case Result of
  72.                   False:
  73.                   begin                                  
  74.                     Result := ((p - 1) <= z);
  75.                     if Result then
  76.                       match := Copy(str, m, ((z - m) + 1));
  77.                   end;
  78.                   True: match := Copy(str, m, (p - m) - 1);
  79.                 end;
  80.               end;
  81.               False:
  82.               begin
  83.                 if x then
  84.                   f := PosEx(a[0], str, 2)
  85.                 else
  86.                   f := Pos(a[0], str);
  87.                 if (f > 0) then
  88.                 begin
  89.                   m := (f + Length(a[0]));
  90.                   if y then
  91.                     p := (z - 1)
  92.                   else
  93.                     p := z;  
  94.                   l := LastPosEx(a[h], str, p);
  95.                   if (l > 0) then
  96.                   begin      
  97.                     if (h > 1) then
  98.                     for i := (h - 1) downto 1 do
  99.                     begin
  100.                       p := LastPosEx(a[i], str, p);
  101.                       if (p < 1) then
  102.                         Exit;
  103.                       p := ((p - Length(a[i])) + 1);
  104.                     end;
  105.                     Result := True;
  106.                     case (x or y) of
  107.                       True:
  108.                       case (x and y) of
  109.                         False:
  110.                         if y then
  111.                           match := Copy(str, f, ((z - f) + 1))
  112.                         else
  113.                           match := Copy(str, 1, ((l + Length(a[h])) - 1));
  114.                         True: match := str;
  115.                       end;
  116.                       False: match := Copy(str, f, (l - f) + Length(a[h]));
  117.                     end;        
  118.                   end;
  119.                 end;
  120.               end;
  121.             end;
  122.           end;
  123.           False:
  124.           begin          
  125.             if x then
  126.               p := PosEx(a[0], str, 2)
  127.             else
  128.               p := Pos(a[0], str);
  129.             if (p > 0) then
  130.               Result := ((((p - 1) + Length(a[0])) < z) or not y);
  131.             if Result then
  132.             case not (x or y) of
  133.               False:  
  134.               case (x and y) of    
  135.                 False:
  136.                 if not x then                            
  137.                   match := (Copy(str, p, (z - p) + 1))
  138.                 else
  139.                   match := (Copy(str, 1, (p - 1)) + a[0]);
  140.                 True: match := str;
  141.               end;
  142.               True: match := a[0];
  143.             end;
  144.           end;        
  145.         end;
  146.       end;
  147.       True: match := str;
  148.     end;    
  149.   end;
  150. end;
  151.  
  152. function StrMaskMatch(expression, str: string; mask: Char; Sensitive: Boolean; var match: string): Boolean;
  153. var
  154.   a: TStrArray;
  155.   s, h, i, l, w, z, f, p, o, m: Integer;  
  156.   e: string;  
  157.   x, y: Boolean;
  158. begin
  159.   match := '';
  160.   Result := False;
  161.   l := Length(expression);
  162.   z := Length(str);
  163.   if ((l > 0) and (z > 0)) then
  164.   begin
  165.     l := (l - 1);
  166.     for i := 1 to l do
  167.       if not ((expression[i] = mask) and (expression[(i + 1)] = mask)) then
  168.         e := (e + expression[i]);
  169.     e := (e + expression[(l + 1)]);  
  170.     a := Explode(mask, e);
  171.     h := High(a);    
  172.     s := 0;      
  173.     w := 0;
  174.     for i := 0 to h do
  175.       if (a[i] <> '') then
  176.       begin
  177.         a[s] := a[i];
  178.         w := (w + Length(a[s]));
  179.         s := (s + 1);
  180.       end;  
  181.     Result := (s = 0);
  182.     case Result of
  183.       False:
  184.       begin      
  185.         x := (e[1] = mask);
  186.         y := (e[Length(e)] = mask);
  187.         case (s > 1) of
  188.           True:
  189.           begin  
  190.             SetLength(a, s);
  191.             h := (s - 1);  
  192.             s := Length(e);
  193.             if ((x or y) and (w > z)) then
  194.               Exit;
  195.             case Sensitive of
  196.               True:
  197.               begin
  198.                 case x of
  199.                   True:
  200.                   begin
  201.                     f := 2;
  202.                     m := 1;
  203.                   end;
  204.                   False:
  205.                   begin
  206.                     f := 1;
  207.                     m := z;
  208.                   end;
  209.                 end;  
  210.                 p := f;  
  211.                 o := 0;
  212.                 for i := 0 to h do
  213.                 begin
  214.                   p := PosEx(a[i], str, p);
  215.                   if (p < 1) then
  216.                     Exit;
  217.                   if (p < m) then
  218.                     m := p;  
  219.                   p := ((p + Length(a[i])) + 1);
  220.                 end;
  221.                 Result := not y;  
  222.                 case Result of
  223.                   False:
  224.                   begin                                  
  225.                     Result := ((p - 1) <= z);
  226.                     if Result then
  227.                       match := Copy(str, m, ((z - m) + 1));
  228.                   end;
  229.                   True: match := Copy(str, m, (p - m) - 1);
  230.                 end;
  231.               end;
  232.               False:
  233.               begin
  234.                 if x then
  235.                   f := PosEx(a[0], str, 2)
  236.                 else
  237.                   f := Pos(a[0], str);
  238.                 if (f > 0) then
  239.                 begin
  240.                   m := (f + Length(a[0]));
  241.                   if y then
  242.                     p := (z - 1)
  243.                   else
  244.                     p := z;  
  245.                   l := LastPosEx(a[h], str, p);
  246.                   if (l > 0) then
  247.                   begin      
  248.                     if (h > 1) then
  249.                     for i := (h - 1) downto 1 do
  250.                     begin
  251.                       p := LastPosEx(a[i], str, p);
  252.                       if (p < 1) then
  253.                         Exit;
  254.                       p := ((p - Length(a[i])) + 1);
  255.                     end;
  256.                     Result := True;
  257.                     case (x or y) of
  258.                       True:
  259.                       case (x and y) of
  260.                         False:
  261.                         if y then
  262.                           match := Copy(str, f, ((z - f) + 1))
  263.                         else
  264.                           match := Copy(str, 1, ((l - Length(a[h])) + f));
  265.                         True: match := str;
  266.                       end;
  267.                       False: match := Copy(str, f, (l - f) + Length(a[h]));
  268.                     end;        
  269.                   end;
  270.                 end;
  271.               end;
  272.             end;
  273.           end;
  274.           False:
  275.           begin          
  276.             if x then
  277.               p := PosEx(a[0], str, 2)
  278.             else
  279.               p := Pos(a[0], str);
  280.             if (p > 0) then
  281.               Result := ((((p - 1) + Length(a[0])) < z) or not y);
  282.             if Result then
  283.             case not (x or y) of
  284.               False:  
  285.               case (x and y) of    
  286.                 False:
  287.                 if not x then                            
  288.                   match := (Copy(str, p, (z - p) + 1))
  289.                 else
  290.                   match := (Copy(str, 1, (p - 1)) + a[0]);
  291.                 True: match := str;
  292.               end;
  293.               True: match := a[0];
  294.             end;
  295.           end;        
  296.         end;
  297.       end;
  298.       True: match := str;
  299.     end;    
  300.   end;
  301. end;
  302.  
  303. var
  304.   Str, expression, Match: string;
  305.  
  306. begin
  307.   ClearDebug;
  308.   expression := 'This* * here*!! ';
  309.   Str := 'This... This is text text right here!!! here is something!?';
  310.   if StrMaskMatch(expression, Str, '*', False, Match) then
  311.     WriteLn('Matched: "' + Match + '"');
  312. end.
  313.  
  314. function StrMaskMatch(expression, str: string; mask: Char; var match: string): Boolean;
  315. var
  316.   a: TStrArray;
  317.   s, h, i, l, w, z, f, p, o, m: Integer;  
  318.   e: string;  
  319.   x, y: Boolean;
  320. begin
  321.   match := '';
  322.   Result := False;
  323.   l := Length(expression);
  324.   z := Length(str);
  325.   if ((l > 0) and (z > 0)) then
  326.   begin
  327.     l := (l - 1);
  328.     for i := 1 to l do
  329.       if not ((expression[i] = mask) and (expression[(i + 1)] = mask)) then
  330.         e := (e + expression[i]);
  331.     e := (e + expression[(l + 1)]);  
  332.     a := Explode(mask, e);
  333.     h := High(a);    
  334.     s := 0;      
  335.     w := 0;
  336.     for i := 0 to h do
  337.       if (a[i] <> '') then
  338.       begin
  339.         a[s] := a[i];
  340.         w := (w + Length(a[s]));
  341.         s := (s + 1);
  342.       end;  
  343.     Result := (s = 0);
  344.     case Result of
  345.       False:
  346.       begin      
  347.         x := (e[1] = mask);
  348.         y := (e[Length(e)] = mask);
  349.         case (s > 1) of
  350.           True:
  351.           begin  
  352.             SetLength(a, s);
  353.             h := (s - 1);  
  354.             s := Length(e);
  355.             if ((x or y) and (w > z)) then
  356.               Exit;  
  357.             case x of
  358.               True:
  359.               begin
  360.                 f := 2;
  361.                 m := 1;
  362.               end;
  363.               False:
  364.               begin
  365.                 f := 1;
  366.                 m := z;
  367.               end;
  368.             end;  
  369.             p := f;  
  370.             o := 0;
  371.             for i := 0 to h do
  372.             begin
  373.               p := PosEx(a[i], str, p);
  374.               if (p < 1) then
  375.                 Exit;
  376.               if (p < m) then
  377.                 m := p;  
  378.               p := ((p + Length(a[i])) + 1);
  379.             end;
  380.             Result := not y;  
  381.             case Result of
  382.               False:
  383.               begin                                  
  384.                 Result := ((p - 1) <= z);
  385.                 if Result then
  386.                   match := Copy(str, m, ((z - m) + 1));
  387.               end;
  388.               True: match := Copy(str, m, (p - m) - 1);
  389.             end;
  390.           end;
  391.           False:
  392.           begin          
  393.             if x then
  394.               p := PosEx(a[0], str, 2)
  395.             else
  396.               p := Pos(a[0], str);
  397.             if (p > 0) then
  398.               Result := ((((p - 1) + Length(a[0])) < z) or not y);
  399.             if Result then
  400.             case not (x or y) of
  401.               False:  
  402.               case (x and y) of    
  403.                 False:
  404.                 if not x then                            
  405.                   match := (Copy(str, p, (z - p) + 1))
  406.                 else
  407.                   match := (Copy(str, 1, (p - 1)) + a[0]);
  408.                 True: match := str;
  409.               end;
  410.               True: match := a[0];
  411.             end;
  412.           end;        
  413.         end;
  414.       end;
  415.       True: match := str;
  416.     end;    
  417.   end;
  418. end;
  419.  
  420. var
  421.   Str, expression, Match: string;
  422.  
  423. begin
  424.   ClearDebug;
  425.   expression := 'text * here';
  426.   Str := 'a ggThis is text text right here here!!! stet f';
  427.   if StrMaskMatch(expression, Str, '*', Match) then
  428.     WriteLn('Matched: "' + Match + '"');
  429. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement