Author: Tomas Rutkauskas
Could someone share some code that would extract all strings between 'start' and
'end'. I'm trying to load a document in TMemo and delete all strings not found
inside a predetermined start and end point.
Answer:
1 2 function TextBetweenStartAndEnd(const Text: string): string;
3 var4 pStart, pEnd: PChar;
5 begin6 {sets a pointer to the "start" position}7 pStart := StrPos(PChar(Text), 'start');
8 if Assigned(pStart) then9 begin10 {sets a pointer behind the "start" position}11 Inc(pStart, Length('start'));
12 {looking for the "end" position}13 pEnd := StrPos(pStart, 'end');
14 {copies the text between the "start" and "end" position}15 if Assigned(pEnd) then16 Result := Copy(string(pStart), 1, pEnd - pStart);
17 end;
18 {if no "start" or "end" then raise an exception}19 if (not Assigned(pStart)) or (not Assigned(pEnd)) then20 raise Exception.Create('Error parsing text!');
21 end;