program ExtractSubstringBetweenTags;
function ExtractContentBetweenTags(const str, tagName: string): string;
var
startTag, endTag: string;
startPos, endPos: Integer;
begin
// Construct the opening and closing tags
startTag := '<' + tagName + '>';
endTag := '</' + tagName + '>';
// Find the position of the opening tag
startPos := Pos(startTag, str);
if startPos > 0 then
begin
// Move past the opening tag to the content
startPos := startPos + Length(startTag);
// Find the position of the closing tag
endPos := Pos(endTag, str);
if endPos > startPos then
begin
// Extract the content between the tags
ExtractContentBetweenTags := Copy(str, startPos, endPos - startPos);
Exit;
end;
end;
// Return an empty string if no match is found
ExtractContentBetweenTags := '';
end;
var
str, content: string;
begin
str := 'abcd <tag>efg hijk lmnop</tag> qrst uvwxyz';
// Call the function to extract the substring
content := ExtractContentBetweenTags(str, 'tag');
if content <> '' then
WriteLn('Extracted content: ', content)
else
WriteLn('No matching tags found.');
end.
(*
run:
Extracted content: efg hijk lmnop
*)