Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1034611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:25:09+00:00 2026-05-16T14:25:09+00:00

How would I go about extracting text between 2 html tags using delphi? Here

  • 0

How would I go about extracting text between 2 html tags using delphi?
Here is an example string.

blah blah blah<tag>text I want to keep</tag>blah blah blah

and I want to extract this part of it.

<tag>text I want to keep</tag>

(basically removing all the blah blah blah garbage that comes before and after the <tag> & </tag> strings which I also want to keep.

Like I said, I am sure this is extremely easy for those who know, but I just cannot wrap my head around it at the moment. Thanks in advance for your replies.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:25:10+00:00Added an answer on May 16, 2026 at 2:25 pm

    This depends entirely on how your input looks.

    Update First I wrote a few solutions for special cases, but after the OP explained a bit more about the details, I had to generalize them a bit. Here is the most general code:

    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;
    
    
    function ExtractTagAndTextInsideGivenTagEx(const Tag, Text: string): string;
    var
      StartPos, EndPos: integer;
    begin
      result := '';
      StartPos := Pos('<' + Tag, Text);
      EndPos := Pos('</' + Tag + '>', Text);
      if (StartPos > 0) and (EndPos > StartPos) then
        result := Copy(Text, StartPos, EndPos - StartPos + length(Tag) + 3);
    end;
    

    Sample usage

    ExtractTextInsideGivenTagEx('tag',
        'blah <i>blah</i> <b>blah<tag a="2" b="4">text I want to keep</tag>blah blah </b>blah')
    

    returns

    text I want to keep
    

    whereas

    ExtractTagAndTextInsideGivenTagEx('tag',
        'blah <i>blah</i> <b>blah<tag a="2" b="4">text I want to keep</tag>blah blah </b>blah')
    

    returns

    <tag a="2" b="4">text I want to keep</tag>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about extracting just the base URl from a long string
Just a quick question about how you would go about implementing this. I want
I would like to know how I would go about altering the HTML that
How would I go about extracting the IL code for classes that are generated
I'm scanning a large data source, currently about 8 million entries, extracting on string
How would I go about extracting the Windows OEM Key from the Registry and
Using C# how should I go about extracting titles subtitles and paragraphs from a
How would I go about extracting the value of a field in a DataGridViewRow,
How would you go about internationalizing a Google App Engine webapp application using BABEL
I'm extracting portions of URLs from text using a regular expression in Python. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.