String manipulation

Please discuss general Delphi programming topics here.

String manipulation

Postby maxa » October 16th, 2011, 9:20 pm

Hi my name is Danijel , and I have problem with extracting string from memo.
I do idhttp1.get for extracting google API results for : http://ajax.googleapis.com/ajax/service ... arge&q=max
I need to extract url's from it so I use:
Code: Select all
function ExtractText(const Str: string; const Delim1, Delim2: string): string;
var
  pos1, pos2: integer;
begin
  result := '';
  pos1 := Pos(Delim1, Str);
  if pos1 > 0 then begin
    pos2 := PosEx(Delim2, Str, pos1+1);
    if pos2 > 0 then
      result := Copy(Str, pos1 + 1, pos2 - pos1 - 1);
  end;
end;


So to extract url :

Code: Select all
memo1.lines.Add((extracttext('"unescapedUrl":"http://www.turkishculturalfoundation.org/pages.php?ID\u003d31","url":"http://www.turkishculturalfoundation.org/pages.php%3FID%3D31","visibleUrl":"www.turkishculturalfoundation.org"','"url":"','","visibleUrl"')))

As you can see I set it to take everything from
Code: Select all
"url":"
to
Code: Select all
","visibleUrl"
but still I get :
Code: Select all
url":"http://www.turkishculturalfoundation.org/pages.php%3FID%3D31

Why url":" isn't cut ?
How can I go through entire memo1 to extract all links , and when I get to the end to show message 'No more links'.
I'm trying to figure this out , but help would be appreciated. Thanks....
maxa
Member
Member
 
Posts: 1
Joined: October 15th, 2011, 9:23 pm

Re: String manipulation

Postby DelphiFanMunich » October 18th, 2011, 10:48 am

Hi Danijel

to your first question I would answer
please use
Code: Select all
result := Copy(Str, pos1 + length(Delim1) , pos2 - pos1 - length(Delim1));


to your second question:

the function ExtractText should have a variable (not a const) Str.
Code: Select all
  function ExtractText (var Str : string; Delim1, Delim2 : string) : string;

and my idea would be to shorten Str from the beginning after you have found some entry
(see later)

The main function would be

Code: Select all
var
  s, s1   : string;
  Delim1  : string;
  Delim2  : string;
  sl      : TStringList;

begin
  s      := '"unescapedUrl":........';       // your search text
  Delim1 := '"url":"';
  Delim2 := '","visibleUrl"';

  repeat
    s1 := ExtractText (s, Delim1, Delim2);
    if s1 <> '' then showMessage (s1);
  until s = '';

The function ExtractText is

Code: Select all
  //----------------------------------------------------------------------------
  function ExtractText (var Str : string; Delim1, Delim2 : string) : string;
  var
    p : integer;
  begin
    result := '';
    p := pos (Delim1, Str);
    if p > 0 then
    begin
      Str := CopyFrom (Str, p + length(Delim1));
      p   := pos (Delim2, Str);
      if p > 0 then
      begin
        result := Copy (Str, 1, p-1);
        Str    := copyFrom (Str, p + length(Delim2));
      end
      else
        Str := '';
    end
    else
      Str := '';
  end;
  //----------------------------------------------------------------------------

and I also use

Code: Select all
//------------------------------------------------------------------------------

function CopyFrom (Line : string; pos : integer) : string;
{
  copies from pos until line-end
}
begin
  Result := Line;
  if (pos > 0) then
    Result := Copy (Result, pos, length(Result) - pos);
end;

//------------------------------------------------------------------------------

I use Delphi 7 may be some of the functions are similar defined in later versions.

This is only a suggestion, disadvantage in your and my code is
- you copy Str, that is time consuming if Str is very long or a stream
it would be better to use pointers with pchar instead
(see "How to match two strings approximately" in this forum).

Basically I would suggest to code object oriented that means to bind
the code in any object

(Sorry for my bad english)

greetings Kornelius
DelphiFanMunich
Member
Member
 
Posts: 2
Joined: October 18th, 2011, 7:10 am

Re: String manipulation

Postby DelphiFanMunich » October 18th, 2011, 12:45 pm

Hi Danijel

thats me again. You are right, it is much easier to use PosEx instead of Pos
(to telling the truth, I have never used it before)

With PosEx the function ExtractText is a little bit easier

Code: Select all
  //----------------------------------------------------------------------------
  function ExtractText (var Str : string ; var Offset : Cardinal;
                            Delim1, Delim2 : string;
                            var Text: string) : boolean;

  var
    p : Cardinal;
  begin
    result := false;
    p := PosEx (Delim1, Str, Offset);
    if p > 0 then
    begin
      Offset := p + length(Delim1);
      p      := PosEx (Delim2, Str, Offset);
      if p > 0 then
      begin
        Text   := Copy (Str, Offset, p - Offset);
        Offset := p + length(Delim2);
        result := true;
      end;
    end;
  end;
  //----------------------------------------------------------------------------

and you don't need any copy of Str.

The main function is then

Code: Select all
var
  s           : string;
  Text      : string;
  Delim1  : string;
  Delim2  : string;
  Offset  : Cardinal;

begin
  s      := '"unescapedUrl":........';       // your search text
  Delim1 := '"url":"';
  Delim2 := '","visibleUrl"';
  Offset := 1;

  while ExtractText (s, Offset, Delim1, Delim2, Text)
     do showMessage (Text);


Kornelius
DelphiFanMunich
Member
Member
 
Posts: 2
Joined: October 18th, 2011, 7:10 am


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron