954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Getting specified HTML/TEXT pattern

Well is there any built-in function to do this ?, or 3rd party library


EXAMPLE code

source := Sock.Get('http://www.google.com/');

if source('Google Search Engine', a) > 0 then 
begin
 NeededPattern := GetPattern(source, '<font>','</font>');
 result := neededPattern; // will display ABC of the <font>ABC</font>
end;

Then it will get the middle of the font HTML result

crapped
Newbie Poster
3 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

There is no builtin function for what you want. It is easy to create though.

Use Pos to find the "" and get the position of the "<". Then add the length of "" to get the position of the desired text.

Then use PosEx to find the position of the first "" after the desired text and calculate the length of the text;

Then use Copy to extract the desired text;

function GetPattern(Source, before, after : String) : String;
  var
    p, l : Integer;
  begin
  p := Pos(Source, before);
  if p < 1 then
    begin
    result := '';
    end
  else
    begin
    p := p + Length(before);
    l := PosEx(Source, after, p);
    if l < 1 then
      begin
      result := '';
      end
    else 
      begin
      l := l - p;
      result := copy(Source, p, l);
      end;
    end;
  end;
dlhale
Newbie Poster
10 posts since Nov 2008
Reputation Points: 12
Solved Threads: 1
 

Delphi includes RegularExpressions with which you can do this, but am not sure from which version. For earlier version TPerlRegex is available (3rd party).

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: