6

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.

3
  • 1
    Oh, the joys of HTML tag soup - if it were XHTML, you could use a XML parser :)
    – mjn
    Aug 30, 2010 at 21:24
  • 4
    Some people, when presented with a problem, decide, "I'll use XML." Now they have four or five problems! Aug 31, 2010 at 0:16
  • If your HTML is not XML, then you cannot reliably parse: it totally depends on the string between the tags, which might have very irregular formatting. If it is HTML, tags themselves might differ in case. Aug 31, 2010 at 1:53

4 Answers 4

8

If you have Delphi XE, you can use the new RegularExpressions unit:

ResultString := TRegEx.Match(SubjectString, '(?si)<tag>.*?</tag>').Value;

If you have an older version of Delphi, you can use a 3rd party regex component such as TPerlRegEx:

Regex := TPerlRegEx.Create(nil);
Regex.RegEx := '(?si)<tag>.*?</tag>';
Regex.Subject := SubjectString;
if Regex.Match then ResultString := Regex.MatchedExpression;
6

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>
7
  • I see what you mean about the formatting and I am sure your code would be great if it were something simple like you said but there are mutliple tags in the same string but the info I want to extract is only between 2 of them. basically the text I am wanting to extract is the embed code of an video which is embedded using the normal <object>embdding code here</object> stuff but the string looks more like I showed in the example above with a bunch of garbage in front of and after the <object> & </object> tags so not sure how the code could be modified to suit that need and thx for the reply
    – fubar
    Aug 30, 2010 at 20:46
  • @fubar: My third and fourth functions can handle tags before/after <tag> and even malformatted HTML code. Aug 30, 2010 at 20:47
  • Sorry about the reply I only saw your first example when I made the reply I will text the others out now thx again all.
    – fubar
    Aug 30, 2010 at 20:56
  • 1
    The last code you provided worked wonderfully. I did not even think about the tag parameters which in my case I needed to account for and your example handled it perfectly. TYVM, Fubar
    – fubar
    Aug 30, 2010 at 21:21
  • The for loop can be replaced by posex in D7+. But I assume this has been done on purpose. Aug 31, 2010 at 8:16
3

you can build an function using the pos the copy functions.

see this sample.

Function ExtractBetweenTags(Const Value,TagI,TagF:string):string;
var
i,f : integer;
begin
 i:=Pos(TagI,Value);
 f:=Pos(TagF,Value);
 if (i>0) and (f>i) then
 Result:=Copy(Value,i+length(TagI),f-i-length(TagF)+1);
end;


Function ExtractWithTags(Const Value,TagI,TagF:string):string;
var
i,f : integer;
begin
 i:=Pos(TagI,Value);
 f:=Pos(TagF,Value);
 if (i>0) and (f>i) then
 Result:=Copy(Value,i,f-i+length(TagF));
end;

and call like this

StrValue:='blah blah blah<tag> text I want to keep</tag>blah blah blah';
NewValue:=ExtractBetweenTags(StrValue,'<tag>','</tag>');//returns 'text I want to keep'
NewValue:=ExtractWithTags(StrValue,'<tag>','</tag>');//returns '<tag>text I want to keep</tag>'
2
  • Notice that (i > 0) and (f > 0) and (f > i) is equivalent to (i > 0) and (f > i), because f > 0 follows from this latter conjunction. Aug 30, 2010 at 20:53
  • I tried the code above but it returned some unexpected results such as text after the closing tag was still being displayed but thx for the reply and your time, much appreciated. Fubar.
    – fubar
    Aug 30, 2010 at 21:22
3

I find that this version is more versatile because it isnt limited to one occurence of the tags. It searches for the next endtag after the starttag.

Function ExtractBetweenTags(Const Line, TagI, TagF: string): string;
var
  i, f : integer;
begin
  i := Pos(TagI, Line);
  f := Pos(TagF, Copy(Line, i+length(TagI), MAXINT));
  if (i > 0) and (f > 0) then
    Result:= Copy(Line, i+length(TagI), f-1);
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.