Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,846 questions

51,767 answers

573 users

How to extract a substring between two tags in Pascal

1 Answer

0 votes
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

*)

 



answered Apr 3, 2025 by avibootz
...