Текст между двумя определёнными строками

Материал из DRKB

Текст между двумя определёнными строками[править | править код]

procedure IsolateText(const S: string; Tag1, Tag2: string; list: TStrings);
var
  pScan, pEnd, pTag1, pTag2: PChar;
  foundText: String;
  searchtext: String;
begin
  { Set up pointers we need for the search. HTML is not case sensitive, so
    we need to perform the search on a uppercased copy of S.}
  searchtext := UpperCase(S);
  Tag1 := UpperCase(Tag1);
  Tag2 := UpperCase(Tag2);
  pTag1 := PChar(Tag1);
  pTag2 := PChar(Tag2);
  pScan := PChar(searchtext);
  repeat
    { Search for next occurence of Tag1. }
    pScan:= StrPos(pScan, pTag1);
    if pScan <> nil then
    begin
      { Found one, hop over it, then search from that position
        forward for the next occurence of Tag2. }
      Inc(pScan, Length( Tag1 ));
      pEnd := StrPos( pScan, pTag2 );
      if pEnd <> nil then
      begin
        { Found start and end tag, isolate text between,
          add it to the list. We need to get the text from
          the original S, however, since we want the un-uppercased
          version! So we calculate the address pScan would hold if
          the search had been performed on S instead of searchtext. }
        SetString(foundText, 
                  PChar(S) + (pScan- PChar(searchtext) ),
                  pEnd - pScan);
        list.Add(foundText);

        { Continue next search after the found end tag. }
        pScan := pEnd + Length(tag2);
      end
      else { Error, no end tag found for start tag, abort. }
        pScan := nil;
    end;
  until pScan = nil;
end;


Source: http://delphiworld.narod.ru
ID: 00925