Solved

Extracting web links from string

Posted on 2004-04-24
8
332 Views
Last Modified: 2010-04-05
Hello,

How can I extract web links from string, for example I have string with web links and other text and I need only those web links that match special code or text.
0
Comment
Question by:H-styler
8 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10907060

I advise regular expressions.

Freeware for Delphi
http://regexpstudio.com/TRegExpr/TRegExpr.html
0
 
LVL 17

Expert Comment

by:mokule
ID: 10907068

This is an example of regular expression from TRegExpr test program for extracting URL from text.

(?i)                          # we need caseInsensitive mode
(FTP|HTTP)://                 # protocol
([_a-z\d\-]+(\.[_a-z\d\-]+)+) # TCP addr
((/[ _a-z\d\-\\\.]+)+)*       # unix path
0
 
LVL 3

Accepted Solution

by:
Kunfufaresi earned 50 total points
ID: 10907298
Hello, not as fancy as regex but you could use this code

s:String;
i :integer;

begin
 s := texttoparse;
 repeat
  i := pos('http://',s);
  if i>0 then
  begin
   delete(s,1,i-1);
   i := pos(' ');
   if i=0 then i := length(s)+1;
   listbox1.items.add(copy(s,1,i-1)); // so the url is = copy(s,1,i-1)
   delete(s,1,i);
  end;
 until i=0;
end;

well this would work if your urls doesnt contain spaces which they shouldnt as spaces are encoded already.

Kunfu Faresi
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 17

Expert Comment

by:mokule
ID: 10907314
I advise regexpr because he wrote
"I need only those web links that match special code or text."
0
 
LVL 3

Expert Comment

by:Kunfufaresi
ID: 10907605
Yes you are right, regexp is much more advanced, but i've used it some times and still have not gotten all the hang of it.
0
 

Author Comment

by:H-styler
ID: 10908088
Kunfufaresi with your code I can`t extract links from this type of string:

<table border=0 cellpadding=0 cellspacing=0 bgcolor=ffffff><tr><td><a href="http://www.bythebeachemails.com/scripts/runner.php?PA=116" target=_ptc onclick="javascript:reloadpage(30)"><img src=http://www.bythebeachemails.com/scripts/runner.php?REDIRECT=http%3A%2F%2Fmpam2.free.fr%2Fcash%2Fbann_cash2.gif alt="Great PTR/PTC sites" width=468 height=60 border=0></a></td></tr></table>The ad above is worth 50 cent(s)<br><br><br><br><br><b>1</b><br> <br>

And I only need to extract those links witch match for example this code: "scripts/runner.php?PA="
0
 
LVL 3

Expert Comment

by:Kunfufaresi
ID: 10908442
Hello,

well if you did a

if pos('scripts/runner.php?PA=',copy(s,1,i-1))>0 then listbox1.items.add(copy(s,1,i-1));

also you should in this case look for pos('"',s) instead of pos(' ',s) as " terminates the url not space.
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 10932036
As mokule said i suggest you to use regular expressions. Here is some usefull stuff http://www.regular-expressions.info/ and if you will search for regular expressions in www.torry.ru, you may found some freeware components.

Regards,
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question