Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to isolate text between two HTML tags Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Reporting /Printing
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius

I have a TRichEdit.Lines (TStrings) where I want to extract a string and copy it to 
another string. I use ScanF to find begining of string which is ''. Then I need to find either next '<' or end 
of Line. Once I do all this, how do I extract this string and copy it to another 


See the Copy function. Perhaps the following routine can be of use for you, it uses 
the diverse PChar-based string functions instead of the standard String Pos and 
Copy, basically because it is a bit easier in this case to work with pointers.
2   procedure IsolateTextBetweentags(const S: string; Tag1, Tag2: string; list: 
3   TStrings);
4   var
5     pScan, pEnd, pTag1, pTag2: PChar;
6     foundText: string;
7     searchtext: string;
8   begin
9     {Set up pointers we need for the search. HTML is not case sensitive, so
10    we need to perform the search on a uppercased copy of S}
11    searchtext := Uppercase(S);
12    Tag1 := Uppercase(Tag1);
13    Tag2 := Uppercase(Tag2);
14    pTag1 := PChar(Tag1);
15    pTag2 := PChar(Tag2);
16    pScan := PChar(searchtext);
17    repeat
18      {Search for next occurence of Tag1}
19      pScan := StrPos(pScan, pTag1);
20      if pScan <> nil then
21      begin
22        {Found one, hop over it, then search from that position forward for the 
23  			next occurence of Tag2}
24        Inc(pScan, Length(Tag1));
25        pEnd := StrPos(pScan, pTag2);
26        if pEnd <> nil then
27        begin
28          {Found start and end tag, isolate text between, add it to the list. We need 
29  to
30          get the text from the original S, however, since we 
31  				want the un-uppercased version!}
32          SetString(foundText, Pchar(S) + (pScan - PChar(searchtext)), pEnd - pScan);
33          list.Add(foundText);
34          {Continue next search after the found end tag}
35          pScan := pEnd + Length(tag2);
36        end
37        else
38          {Error, no end tag found for start tag, abort}
39          pScan := nil;
40      end;
41    until
42      pScan = nil;
43  end;
45  procedure TForm1.Button1Click(Sender: TObject);
46  begin
47    with opendialog1 do
48    begin
49      filter := 'HTML files|*.HTM; *.HTML';
50      if execute then
51      begin
52        richedit1.PlainText := true;
53        richedit1.lines.loadfromfile(filename);
54        memo2.clear;
55        IsolateTextBetweenTags(richedit1.text, '<H1>', '</H1>', memo2.lines);
56      end;
57    end;
58  end;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC