德尔福Pos的位置

时间:2017-04-29 03:10:37

标签: delphi

所以我有以下代码可行:

function TForm2.SearchForm(Count: Integer): String;
var
  i,a,a1: integer;
  s,s0,s1,s2: string;
begin
  s := Memo1.Text;
  for i := 0 to Count-1 do
  begin
    a:=Pos('"last_ten_starts":"',s);
    s0:=Copy(s, 1, a);
    Delete(s, 1, a+18);
    a:=Pos('"', s);
    s1:=Copy(s, 1, a-1);//copy last 10 runs
    s2:=Copy(s1, Length(s1)-3, Length(s1));//get last 4 runs
    Memo1.Lines.Append('Form (x10): '+s1+ ' Form (x4): '+s2);
    if (s2 = '1111') or (s2 = '111x') or (s2 = '11x1') or (s2 = '1x11') or (s2 = 'x111') then
    begin
      Log(s0);
      ShowMessage('Pos: '+IntToStr(a));
    end;
  end;
end;

function TForm2.GetRaceNumber(Count: Integer): string;
var
  s, s1: string;
  i, a, a1: integer;
begin
  s := Memo1.Text;
  for i := 0 to Count-1 do
  begin
    a:=Pos('"RaceForm":{"@number":"',s);
    Copy(s, 1, a);
    Delete(s, 1, a+22);
    a1:=Pos('"', s);
    s1:=Copy(s, 1, a1-1);//copy race number
  end;
end;

一切都很好,代码工作,有点凌乱,但整理后可以发生。我对如何做某事感到有点困惑。

令我困惑的是,我甚至在努力解释我需要尝试做什么。

所以“RaceForm”:{“@ number”:“有8次出现,在这些事件之间约为90次”last_ten_starts“:”。我需要做的是检查RaceForm的所有8个位置,以确定last_ten_starts是否是第1到第8场比赛。

这有意义吗?

编辑:所以这是我的文字示例(顺便说一句,我正在阅读html备忘录和搜索文本)

RaceNumber: 1 Position: 23622
RaceNumber: 2 Position: 37026
RaceNumber: 3 Position: 74937
RaceNumber: 4 Position: 81592
RaceNumber: 5 Position: 82517
RaceNumber: 6 Position: 90886
RaceNumber: 7 Position: 95888
RaceNumber: 8 Position: 99938

所以在位置1和位置2之间,我想要读取所有“last_ten_starts”并将它们设置为Race 1. 2和3之间的位置“last_ten_starts”将是第2场。

在最后十次开始的那一刻看起来像这样:

Form (x10): 6x49559545 Form (x4): 9545
Form (x10): 80x1071684 Form (x4): 1684
Form (x10): 9185215877 Form (x4): 5877
Form (x10): 5530017849 Form (x4): 7849
Form (x10): 4x83373x56 Form (x4): 3x56
Form (x10): 5970636148 Form (x4): 6148
Form (x10): 8x25785511 Form (x4): 5511
Form (x10): 668670x763 Form (x4): x763
Form (x10): x43530x956 Form (x4): x956
Form (x10): 83x2x11113 Form (x4): 1113
Form (x10): 15x3214533 Form (x4): 4533
Form (x10): 2149x8x830 Form (x4): x830
Form (x10): 6127x46325 Form (x4): 6325

但是希望代码在正方形块之间读取它们并将它们放置为:

Race 1: Form (x10): 6x49559545 Form (x4): 9545
Race 1: Form (x10): 80x1071684 Form (x4): 1684
Race 1: Form (x10): 9185215877 Form (x4): 5877
Race 2: Form (x10): 5530017849 Form (x4): 7849
Race 2: Form (x10): 4x83373x56 Form (x4): 3x56
Race 3: Form (x10): 5970636148 Form (x4): 6148
Race 4: Form (x10): 8x25785511 Form (x4): 5511
Race 4: Form (x10): 668670x763 Form (x4): x763
Race 5: Form (x10): x43530x956 Form (x4): x956
Race 6: Form (x10): 83x2x11113 Form (x4): 1113
Race 7: Form (x10): 15x3214533 Form (x4): 4533
Race 8: Form (x10): 2149x8x830 Form (x4): x830
Race 8: Form (x10): 6127x46325 Form (x4): 6325

1 个答案:

答案 0 :(得分:2)

您可以同时扫描两种模式的文本。我建议使用PosEx从最后一个位置继续搜索而不删除字符串。伪代码:

RaceNum := 0;
LastPos := 1;
RacePos := PosEx('"RaceForm":{"@number":"', s, LastPos);
TenPos := PosEx('"last_ten_starts":"', s, LastPos);

while (RacePos > 0)  or (TenPos > 0) do begin

   if (RacePos > 0) then
      if (TenPos = 0) or (RacePos < TenPos) then begin
          Extract RaceNumber from race record
          //Update LastPos to the end of race record:
          LastPos := LastPos + Length('"RaceForm":{"@number":"');   
       end;

   if (TenPos > 0) then
      if (RacePos = 0) or (RacePos > TenPos) then begin
          Extract last_ten record
          Assign it to the current RaceNumber
          //Update LastPos to the end of last_ten record
          LastPos := LastPos + Length('"last_ten_starts":"');   
       end;

   RacePos := PosEx('"RaceForm":{"@number":"', s, LastPos);
   TenPos := PosEx('"last_ten_starts":"', s, LastPos);
end;