Advertisement
Janilabo

asd

Nov 30th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.30 KB | None | 0 0
  1. function StrMatchesEx(ignore: Char; s, str: string; sensitive: Boolean; var position: Integer): string;
  2. var
  3.   t: TStringArray;
  4.   h, i, l, p, f: Integer;
  5.   d: string;
  6. begin
  7.   position := 0;
  8.   Result := '';
  9.   l := Length(s);
  10.   if ((l > 0) and not (l > Length(str))) then
  11.   begin
  12.     d := StringOfChar(ignore, 1);
  13.     t := Explode(d, s);
  14.     h := High(t);
  15.     f := Pos(t[0], str);
  16.     l := (f + Length(t[0]));
  17.     if ((h > 0) and (f > 0)) then
  18.     begin
  19.       case sensitive of
  20.         True:
  21.         for i := 1 to h do
  22.         begin
  23.           p := PosEx(t[i], str, l);
  24.           if (p > 0) then
  25.             l := (p + Length(t[i]))
  26.           else
  27.             Exit;
  28.         end;
  29.         False:
  30.         for i := 1 to h do
  31.         begin
  32.           p := LastPos(t[i], str);
  33.           if (p > l) then
  34.             l := (p + Length(t[i]))
  35.           else
  36.             Exit;
  37.         end;  
  38.       end;  
  39.       position := f;
  40.       Result := Copy(str, f, (l - f));      
  41.     end;
  42.   end;
  43. end;
  44.  
  45. function StrMatches(ignore: Char; s, str: string; sensitive: Boolean): string;
  46. var
  47.   t: TStringArray;
  48.   h, i, l, p, f: Integer;
  49.   d: string;
  50. begin
  51.   Result := '';
  52.   l := Length(s);
  53.   if ((l > 0) and not (l > Length(str))) then
  54.   begin
  55.     d := StringOfChar(ignore, 1);
  56.     t := Explode(d, s);
  57.     h := High(t);
  58.     f := Pos(t[0], str);
  59.     l := (f + Length(t[0]));
  60.     if ((h > 0) and (f > 0)) then
  61.     begin
  62.       case sensitive of
  63.         True:
  64.         for i := 1 to h do
  65.         begin
  66.           p := PosEx(t[i], str, l);
  67.           if (p > 0) then
  68.             l := (p + Length(t[i]))
  69.           else
  70.             Exit;
  71.         end;
  72.         False:
  73.         for i := 1 to h do
  74.         begin
  75.           p := LastPos(t[i], str);
  76.           if (p > l) then
  77.             l := (p + Length(t[i]))
  78.           else
  79.             Exit;
  80.         end;  
  81.       end;  
  82.       Result := Copy(str, f, (l - f));      
  83.     end;
  84.   end;
  85. end;
  86.  
  87. var
  88.   position: Integer;
  89.   str, expr: string;
  90.  
  91. begin
  92.   str := 'hello world world world, cheese!';
  93.   ClearDebug;
  94.   expr := StrMatches('*', 'hello*world', str, False);
  95.   if (expr <> '') then
  96.     WriteLn('"' + expr + '"');
  97.   expr := StrMatchesEx('*', 'hello*world', str, False, position);
  98.   if (expr <> '') then
  99.     WriteLn('"' + expr + '" @' + IntToStr(position));
  100. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement