Delphi extract string between tags twice



I found this code as I run into the same poblem: retrieving the text between two tags. It works, but it only grabs the first occurrance of hello.



function ExtractTextInsideGivenTagEx(const Tag, Text: string): string;
var
StartPos1, StartPos2, EndPos: integer;
i: Integer;
begin
result := '';
StartPos1 := Pos('<' + Tag, Text);
EndPos := Pos('</' + Tag + '>', Text);
StartPos2 := 0;
for i := StartPos1 + length(Tag) + 1 to EndPos do
if Text[i] = '>' then
begin
StartPos2 := i + 1;
break;
end;


if (StartPos2 > 0) and (EndPos > StartPos2) then
result := Copy(Text, StartPos2, EndPos - StartPos2);
end;


But what If you have


<sample>hello1</sample><sample>hello2</sample><sample>hello3</sample><sample>hello4</sample>


and you want to grab the text of the first TWO occurrances ?


No comments:

Post a Comment