Janilabo

Janilabo | MultiBetweenEx() [Simba]

Jun 20th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.57 KB | None | 0 0
  1. function MultiBetweenEx(str, s1, s2: string; method: Integer): array of string;
  2. var
  3.   s1L, s2L, strL, s, o, n, e, r: Integer;
  4. begin
  5.   strL := Length(str);
  6.   s1L := Length(s1);
  7.   if (strL <= Length(s1 + s2)) or (s1L < 1) or (Length(s2) < 1) or not InRange(method, 0, 3) then
  8.     Exit;
  9.   SetLength(Result, (strL - (s1L + s2L)));
  10.   case method of
  11.     0: repeat
  12.          n := PosEx(s1, str, (n + 1));
  13.          if (n < 1) then
  14.            Break;
  15.          e := PosEx(s2, str, (n + 1));
  16.          if (e < 1) then
  17.            Break;
  18.          repeat
  19.            o := n;
  20.            n := PosEx(s1, str, (n + 1));
  21.          until (n >= e) or (n < 1);
  22.          n := o;
  23.          Result[r] := Between(s1, s2, Copy(str, n, (s1L + (e + s2L))));
  24.          if (Result[r] <> '') then
  25.            Inc(r);
  26.        until (e < 1) or (n < 1);
  27.     1..3: repeat
  28.             case method of
  29.               1, 2: s := PosEx(s1, str, (e + 1));
  30.               3: s := PosEx(s1, str, (s + 1));
  31.             end;
  32.             e := PosEx(s2, str, (s + 1));
  33.             if (s < 1) or (e < 1) then
  34.               Break;
  35.             Result[r] := Copy(str, (s + s1L), (e - (s + s1L)));
  36.             if (Result[r] <> '') then
  37.               Inc(r)
  38.             else
  39.               if (method = 1) then
  40.                 e := ((s + s1L) - 1);
  41.           until (s < 1) or (e < 1);
  42.   end;
  43.   SetLength(Result, r);
  44. end;
  45.  
  46. var
  47.   m, h, i: Integer;
  48.   TSA: array of string;
  49.   Text: string;
  50.  
  51. begin
  52.   ClearDebug;
  53.   for m := 0 to 3 do
  54.   begin
  55.     WriteLn('*** METHOD ' + IntToStr(m) + ' ***');
  56.     WriteLn('MultiBetweenEx() Test1:');
  57.     Text := '<tag>test1<tag>..<tag>test2dgtsgtsgdsgs<tag>....<tag>test3<tag>.....<tag>test4<tag>.....';
  58.     TSA := MultiBetweenEx(Text, '<tag>', '<tag>', m);
  59.     h := High(TSA);
  60.     for i := 0 to h do
  61.       WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
  62.     WriteLn('');
  63.     SetLength(TSA, 0);
  64.     WriteLn('MultiBetweenEx() Test2:');
  65.     Text := '<tag>test1</tag>....<tag>test2dgtsgtsgdsgs</tag>....<tag>test3</tag>.....<tag>test4</tag>.....';
  66.     TSA := MultiBetweenEx(Text, '<tag>', '</tag>', m);
  67.     h := High(TSA);
  68.     for i := 0 to h do
  69.       WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
  70.     WriteLn('');
  71.     SetLength(TSA, 0);
  72.     WriteLn('MultiBetweenEx() Test3:');
  73.     Text := '||*|*||<|<||[|]||>|>||*|*||';
  74.     TSA := MultiBetweenEx(Text, '|', '|', m);
  75.     h := High(TSA);
  76.     for i := 0 to h do
  77.       WriteLn('TSA[' + IntToStr(i) + ']: ' + TSA[i]);
  78.     SetLength(TSA, 0);
  79.     WriteLn('*** METHOD ' + IntToStr(m) + ' ***');
  80.     if (m < 4) then
  81.       WriteLn('');
  82.   end;
  83. end.
Advertisement
Add Comment
Please, Sign In to add comment